Warning: Attempt to assign property of non-object, but the Object exists
You are encountering a classic PHP dynamic property trap where the logic for creating nested objects depends on how you access them. The error Warning: Attempt to assign property of non-object on $networks->test->enabled->type occurs because your code path attempts to write to $this->properties[$name] without verifying that it is actually an object instance at that moment.
In your __set method, you distinguish between existing properties and new ones:
public function __set($name, $value) {
if(isset($this->{$name})) {
$this->{$name} = $value;
} else {
// This branch runs when the property is accessed via getter first
$this->properties[$name] = new self($value, null);
}
}
The problem lies in your __get method:
public function __get($name) {
return $this->properties[$name]->value;
}
When you execute $networks->test->enabled = true;, PHP first calls __get('enabled'). Since 'enabled' does not exist in the $properties array yet, this triggers an undefined index notice or returns null depending on your server settings, but more critically, it assumes the structure is already there. However, looking closer at your specific error trace and flow:
Actually, the issue is simpler than a missing object; it is about when the property gets created versus how you access it.
Let's trace the execution of $networks->test->enabled = true;:
- PHP sees
$networks->test. This calls__get('test')on the\Orange_Post\Networkinstance if accessed via getter syntax, but here we are assigning to a property of an external object. Wait,$networksis astdClass, not yourNetworkclass. - You assign
$networks->test = new \Orange_Post\Network('Test');. This creates the first object correctly. - You then access
$networks->test->enabled. Sinceenabledis not a declared property in yourNetworkclass, PHP triggers__get('enabled'). - Inside
__get, you try to return$this->properties['enabled']->value. But'enabled'has never been added to the$propertiesarray because__setwas never called on the object yet! The__getmethod assumes that if you are trying to read a property, it exists in the internal storage. If it doesn't, accessing$this->properties[$name]returnsnull(or throws an error depending on configuration), and then calling->valueonnullis valid, but returning that value creates a state where the property is effectively null.
However, the specific error you are seeing happens on the assignment line: $networks->test->enabled->type = 'boolean';.
This implies that when you access $networks->test->enabled, your __get method returned something that isn't an object, or the logic inside __set wasn't triggered correctly to create the intermediate object.
Let's look at the specific flow again. You are trying to build a nested structure where reading a property automatically creates an instance of itself if it doesn't exist yet (a "fluent" interface pattern).
The bug is in your __get method:
public function __get($name) {
return $this->properties[$name]->value; // CRASH HERE
}
If 'enabled' is not in $this->properties, PHP throws a "Trying to get property of non-object" (or undefined index warning). But wait, if __get crashes, how does the assignment work?
Ah, the actual flow for $networks->test->enabled = true; is:
- PHP calls
__set('enabled', true)on the$networks->testobject. - Inside
__set, you checkif(isset($this->{$name})). Since we haven't read it yet, this is false. - You enter the
elseblock:$this->properties[$name] = new self($value, null);. This creates a newNetworkobject with valuetrue. - Now the property exists in the internal array.
So far so good. The next line is $networks->test->enabled->type = 'boolean';.
- PHP calls
__get('enabled')on the first Network object. - Inside
__get, you access$this->properties['enabled']. This returns theNetworkobject created in step 3 above. - You then try to return
->value. That works fine.
Wait, if that works, why the error? The error says "Attempt to assign property of non-object". This happens on the assignment side: $...->enabled->type = 'boolean'.
This means PHP is treating $networks->test->enabled as a variable that needs to be assigned to, rather than an object. But your __get returns a value, not an object reference unless you wrap it? No, in PHP, accessing a property of an object returns the value. If you want to assign to a dynamic property of the returned object, the getter must return an object.
Your __get returns $this->properties[$name]->value.
If $this->properties[$name] is a Network object, then ->value is a scalar (e.g., true).
You cannot assign to .type on a boolean true. You need the getter to return the object itself, so that subsequent assignments modify the object's properties.
The fix is to return the object from __get, not its value. If you want to expose the value, you should use a dedicated property access method, but for dynamic assignment chains, returning the instance is required.
Here is the corrected logic:
public function __get($name) {
if (!isset($this->properties[$name])) {
// Defensive programming: handle missing properties gracefully or create them?
// Based on your intent, you likely want to create it here too.
$this->properties[$name] = new self(null, null);
}
return $this->properties[$name]; // Return the object, not $value
}
public function __set($name, $value) {
// If the property doesn't exist, initialize it as an object first
if (!isset($this->properties[$name])) {
$this->properties[$name] = new self($value, null);
} else {
$this->{$name} = $value;
}
}
By returning the object instance from __get, the line $networks->test->enabled->type = 'boolean' assigns to a dynamic property of that returned object, which is exactly what PHP expects.
Trade-offs and Considerations
This pattern allows for highly dynamic data structures without defining every possible property upfront. However, it comes with significant downsides:
- Debugging: It is difficult to inspect the internal state using standard tools because properties are hidden in
$propertiesrather than declared. - Type Safety: You lose compile-time type checking. PHP will only catch errors at runtime.
- Performance: Every access involves method calls and array lookups instead of direct property access.
If you are building a library like this for internal use, ensure that the return values from getters are consistent (always objects if the chain continues). If you need to expose raw values frequently, consider adding a getValue($name) method instead of relying on __get for everything.
For robust application development, especially within the Laravel ecosystem, using Eloquent models or typed properties usually provides better maintainability than this dynamic approach. You might find similar patterns discussed in advanced PHP tutorials where dynamic property handling is explored further.