How to add property to object in PHP >= 5.3 strict mode without generating error
In PHP >= 5.3, you can use the Object::$strictProperties
property to enable or disable strict property checking for an object. When this property is set to true
, you will get an error if you try to add a new property to the object that wasn't previously declared. If you want to add a new property to the object without generating an error, you can set Object::$strictProperties
to false
before adding the property.
For example:
<?php
class MyClass
{
public $strictProperties = false;
public $foo;
}
$obj = new MyClass();
// This will not generate an error because strictProperties is set to false
$obj->bar = 'baz';
echo $obj->bar; // Outputs: "baz"
Keep in mind that if you set Object::$strictProperties
to false
, you may not get an error if you try to access a property that doesn't exist, which can make it difficult to debug your code.