PHP - Indirect modification of overloaded property
When you rely on magic methods like __get() to implement lazy loading, your class often acts as a transparent proxy for underlying data. The pattern is straightforward: check if a property exists in an internal cache, and if not, instantiate the required object and return it. This approach keeps memory usage low until a specific attribute is accessed.
However, reading and writing properties behave differently under the hood. Consider a mapper class where $user->role triggers the creation of a Role object via __get(). Reading $user->role->rolename works perfectly fine because the magic method simply returns the existing instance of the Role object. The subsequent access to rolename resolves to that specific instance's property.
The complication arises when you attempt to modify that property directly:
$user = createUser();
$user->role->rolename = 'Test';
This line triggers a PHP Notice: Indirect modification of overloaded property has no effect. This is not a bug in the language engine; it is an intentional safeguard. PHP treats the result of __get() as a temporary value for the purpose of that specific statement rather than a stable reference to a persistent object property.
In standard PHP, when you execute $obj->prop = 'value', the engine expects $obj to be a direct reference to a variable holding an object. When you chain access through __get(), the parser interprets the left-hand side of the assignment as an overloaded expression. Because the magic method returns a value rather than exposing a writable link to a stored instance within the proxy class, PHP refuses to perform the write operation on the target property.
Interestingly, if you inspect the object after the failed assignment, the change might appear to have taken effect depending on how the underlying data is managed. The source material highlights a specific edge case where returning an array of objects from __get() exacerbates this issue, though the core behavior remains consistent: direct assignment fails with a notice.
To resolve this, you must bypass PHP's protection mechanism by explicitly retrieving the object and assigning to it in two steps. Instead of chaining the assignment through the magic method, you fetch the instance first and then modify its properties:
$role = $user->role; // Triggers __get() and retrieves the object
$role->rolename = 'Test'; // Now modifying a direct property reference
By separating the retrieval from the modification, you provide PHP with a clear, non-overloaded variable to write against. The __get() method executes once, storing the result in $role. Subsequent operations on $role are treated as standard property access, and no "indirect modification" notice is generated.
This distinction is crucial when building mappers or DTOs that manage complex object graphs. If your architecture requires state changes to be reflected immediately after accessing a lazy-loaded child, you must ensure the assignment target is resolved before the write occurs. Failing to do so results in silent failures where the code runs without error but leaves the data unchanged in persistent storage.
If you are working within the Laravel ecosystem, this pattern can also apply when dealing with Eloquent relations that are eager-loading or accessed via ->relation. While Laravel's magic methods handle hydration differently, understanding the fundamental limitation of indirect modification helps prevent subtle bugs where form inputs or validation updates fail to persist simply because of how the property was accessed.
