fer79/php-optional-helper

Optional helper similar to Laravel, compatible with arrays and objects. Initially thinked to use in Yii2, should work in other projects.

Maintainers

Package info

github.com/fer79/php-optional-helper

pkg:composer/fer79/php-optional-helper

Statistics

Installs: 15

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2025-09-12 17:07 UTC

This package is auto-updated.

Last update: 2026-03-15 13:58:40 UTC


README

A small utility to implement a helper optional() similar to the one in laravel compatible with arrays, objects, methods and supports ArrayAccess.

🔒 Limitations

  • Nonexistent properties and methods return null.
  • The object is read-only (offsetSet and offsetUnset do nothing).

✨ Available Methods

  • Property access

      optional($obj)->prop
  • Method access

      optional($obj)->method()
  • Safe array access

      optional($array)['key']
  • Get internal value

      optional($value)->get()
  • String casting

      echo optional($value);
    
      null → ""
      string / int / float / bool → converted to string
      arrays / objects → JSON

📜 License MIT License © Fernan Gonzalez