How can I return null for a property and its children keys that are not set in a class?
I can use __get
to make the property to return null
but I have no idea how I can do it for its children keys. Any ideas?
class MyClass
{
public $foo;
public $bar;
public function __construct()
{
$this->bar = (object) ['tool' => 'Here we go'];
}
public function __get($name)
{
return (isset($this->$name)) ? $this->$name : null;
}
}
$class1 = new MyClass;
var_dump($class1->foo); // null
var_dump($class1->boo); // null
var_dump($class1->bar->tool); // 'Here we go'
var_dump($class1->bar->mars); // Notice: Undefined property: stdClass::$mars in...
Is it possible to get null
for $class1->bar->mars
?