How can I use __get () to return null in a property of a multilevel object, referring to a case like the one below?
For example, these are my classes,
class property
{
public function __get($name)
{
return (isset($this->$name)) ? $this->$name : null;
}
}
class objectify
{
public function array_to_object($array = array(), $property_overloading = false)
{
if (!is_array($array)) $array = array($array);
if($property_overloading === true) $object = new property();
else $object = new stdClass();
foreach($array as $key => $value)
{
$key = (string) $key ;
$object->$key = is_array($value) ? self::array_to_object($value, $property_overloading) : $value;
}
return $object;
}
}
How do i use it
$object = new objectify();
$type = array(
"category" => "admin",
"person" => "unique",
"a" => array(
"aa" => "xx",
"bb"=> "yy"
),
"passcode" => false
);
$type = $object->array_to_object($type,true);
var_dump($type->a->cc);
result
null
but I get an error with NULL when the input array null,
$type = null;
$type = $object->array_to_object($type,true);
var_dump($type->a->cc);
result
Notice: Trying to get property of non-object in C:\wamp\www\test...p on line 68
NULL
Is it possible to return NULL in this script?
source
share