Exceptions and edge cases
PropertyDoesNotExistException
This exception is thrown when a property is read or written and the trait cannot find a matching getter or setter.
$example->unknown;
$example->unknown = "value";
Both cases throw GT\PropFunc\PropertyDoesNotExistException if no matching magic property methods exist.
PropertyReadOnlyException
This exception is thrown when:
- a getter exists but a setter does not, and we try to write the property
unset()is called on a property with no setter
$example->id = 123;
unset($example->id);
If id only has __propGetId(), both operations are treated as attempts to modify a read-only property.
PropertyReadOnlySetMoreThanOnceException
This class exists in the library, but the trait does not throw it directly.
It is there for application code or higher-level libraries that want a more specific exception type for write-once rules.
In other words, if you need “can be written once, then becomes read-only”, that policy belongs in your setter method.
property_exists() is not the same as magic property support
property_exists($object, "name") only answers whether the class declares a real property called name.
It does not tell us whether MagicProp can respond to $object->name.
For example:
- a public or private declared property may make
property_exists()returntrue - a fully magic property backed only by
__propGetName()may makeproperty_exists()returnfalse
So property_exists() is about declared class structure, not the full property-like API exposed by the trait.
isset() tells us whether the property is readable
This is worth repeating because it can surprise people.
If a getter exists, isset($object->property) returns true even when:
- the getter would return
null - the backing value has not really been initialised yet
That makes isset() more like “is this property available?” than “does this property currently contain a value?”.
unset() is rarely the best public API
unset() only makes sense when the property maps cleanly to real mutable state.
For most MagicProp code, a clearer alternative is to:
- expose a setter that accepts
null - provide a dedicated method such as
clearNickname()
That usually makes the class easier to understand, especially for newer developers.
Finally, check some Examples for full reference classes.