The Hidden Property Bag: Why __get and __set Fail on Public Attributes in PHP 5.3
You are encountering a common misconception about PHP's magic methods. The __get and __set methods do not act as a universal intercept for every property access or assignment. They specifically target properties that the engine treats as non-existent at the time of the operation.
In your example, you define $bar as a public property:
public $bar;
Once this line executes, the property bar exists in the object's memory space. When you run $foo->bar, the engine checks the object's internal data structure. It finds bar. Because the property is found, the engine bypasses the magic method entirely and returns the value directly. Similarly, when assigning $foo->bar = 'test', the engine sees an existing public slot and writes to it without invoking __set.
This behavior is not a bug in PHP 5.3.3; it is the intended design of the language specification. Magic methods are designed for dynamic property handling, such as accessing properties that do not yet exist or for enforcing strict data patterns on specific attributes. They are not meant to replace standard getter and setter logic for declared members.
If you require custom logic whenever a property is accessed or modified, you must declare the property dynamically inside the magic methods themselves. This approach ensures the engine never finds the property in the object's default namespace, forcing it to route the call to your logic.
Implementing Dynamic Property Logic
To make __get and __set trigger consistently, you should remove the explicit property declaration from the class scope and handle the storage within the magic methods. Here is how you can refactor your code to achieve the desired behavior:
class Foo {
// Remove 'public $bar;' here
public function __get($name) {
echo "Get: $name";
// You must store and retrieve values dynamically
// typically in a protected array or similar structure
if (isset($this->$name)) {
return $this->$name;
}
// Return null or throw an exception if the property isn't set
return null;
}
public function __set($name, $value) {
echo "Set: $name to $value";
// Store the value dynamically
$this->$name = $value;
}
// Optional: Add __isset and __unset for completeness
public function __isset($name) {
return isset($this->$name);
}
public function __unset($name) {
unset($this->$name);
}
}
$foo = new Foo();
// Now this will trigger __set
$foo->bar = 'test';
// And this will trigger __get
echo $foo->bar;
By removing the static declaration, $this->$name becomes a dynamic assignment. The engine no longer finds bar in the class definition, so it falls back to your custom implementation.
Trade-offs and Best Practices
Switching to dynamic properties changes the nature of your class significantly. This pattern effectively turns your class into a proxy or a bag of data where validation logic lives entirely within the magic methods.
The primary trade-off is performance. Magic method calls in PHP are slower than direct property access because they involve function lookup and execution overhead. If you have high-performance requirements, accessing public properties directly is always faster.
Another critical consideration is debugging and tooling. When properties are dynamic, IDEs often cannot provide accurate autocomplete or type hints for those attributes until the data is actually set. Static analysis tools may also struggle to infer the shape of your object if all properties are handled dynamically.
If you need both performance and validation, consider using private properties alongside standard getters and setters. This allows you to maintain a clear contract while avoiding magic method overhead for internal operations. Magic methods should be reserved for scenarios where property names are not known at compile time or when implementing specific dynamic data structures.
