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.
v1.0.0
2025-09-12 17:07 UTC
Requires
- php: >=7.4
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