I am apparently late in coming to this trick, but PHP 8.1 is going to make it even nicer to use.
A clever trick
There's an interesting intersection of functionality in PHP:
- Declared object properties are more efficient than dynamic ones, because the engine can make assumptions about what data type to expect.
- The magic `__get()` and `__set()` methods trigger when there is no property with a given name that has been *set*.
- Fun fact: A property that has been declared but not initialized with a value is still technically "set"... to `uninitialized`.
- However, you can `unset()` an uninitialized property.
That means you can do clever tricks like this (and some systems do, internally):
Continue reading this post on PeakD