Indirect Modification of Overloaded Property: The Array Assignment Trap
When you implement a custom class that mimics array access via __get and __set magic methods, PHP's behavior changes subtly depending on how you write the assignment. Consider this common scenario where you want to store collections in a shared registry:
Registry::getInstance()->foo = [1, 2, 3]; // Works fine
Registry::getInstance()->foo[] = 4; // Triggers "Indirect modification of overloaded property has no effect"
The first line works because the left-hand side is a direct assignment to a property. The engine evaluates __get('foo'), retrieves the existing value, and then assigns the new array reference. However, the second line fails with a specific notice: "Indirect modification of overloaded property has no effect." This isn't a bug in your code; it's a deliberate limitation in how PHP handles magic methods during array append operations.
Why the Notice Appears
The core issue lies in the evaluation order and the nature of the operation. When you execute $obj->foo[] = 4, PHP cannot simply treat this as "get foo, then push an element." Instead, the engine attempts to perform the modification directly on the property descriptor before resolving the value through your magic method.
In a standard array, $arr[] = 4 is an atomic operation that increments the internal count and appends the value. When a property is overloaded via __get, PHP tries to bypass the getter to optimize this specific syntax. Since your class does not provide a way to modify the underlying storage atomically within the __set context for empty arrays, the engine detects that it cannot guarantee the operation succeeded on the actual stored data. It issues the notice and leaves the array unchanged.
Essentially, $obj->foo[] = 4 is syntactic sugar for "append to the result of __get('foo')". If __get returns an existing array, you might expect it to work, but PHP's internal logic for overloaded properties restricts this optimization. The engine assumes that if you are modifying the property indirectly via an append, you should use a standard assignment instead.
The Correct Approach
To resolve this, you must bypass the magic method trap and treat the retrieved value as a local variable. Retrieve the array first, modify it, then assign it back. This ensures the modification happens on a concrete instance of data rather than through the overloaded property descriptor.
// Get the existing array (or null)
$existing = Registry::getInstance()->foo;
// Create a new array with the appended element
$newData = is_array($existing) ? [...$existing, 4] : [4];
// Assign it back explicitly
Registry::getInstance()->foo = $newData;
This pattern guarantees that the append operation occurs on a standard PHP array, avoiding the ambiguity of the overloaded property syntax. While slightly more verbose, it removes any doubt about whether the data was actually modified in the registry.
Design Trade-offs
Implementing a Registry or Singleton with magic methods is often a quick way to build a simple dependency container without external dependencies. However, this approach introduces fragility. Magic methods like __get and __set hide the internal state, making it impossible to enforce type safety or validation rules during direct property assignment.
For example, if you wanted to ensure that $foo is always an array, a magic setter could validate the input before storing it. However, because of the limitations shown above, you cannot rely on __set to handle array operations like pushing elements or resetting indices via $obj->arr[] = 1. The magic methods act as a thin wrapper that breaks when PHP expects atomic array behavior.
If your application grows beyond simple storage needsârequiring serialization, type enforcement, or complex state managementâa dedicated dependency injection container is usually the better choice. Tools like Laravel's service container handle these interactions without relying on magic methods, avoiding these specific pitfalls entirely. For more details on modern dependency management patterns, you can explore resources from https://laravelcompany.com.
Conclusion
The "Indirect modification of overloaded property has no effect" notice is a signal to stop trying to use array shorthand syntax with overloaded properties. Always retrieve the value, perform the mutation locally, and assign it back. While this adds a few lines of code, it ensures your registry behaves predictably and avoids silent data corruption caused by PHP's internal optimizations.