How to use __get () to return null in the properties of a multilevel object?

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 $array is not an array, let make it array with one value of former $array.
        if (!is_array($array)) $array = array($array);

        # Use property overloading to handle inaccessible properties, if overloading is set to be true.
        # Else use std object.
        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?

+3
source share
3 answers

You can return a new property instead of null

 public function __get($name)
    {
        return (isset($this->$name)) ? $this->$name : new property();
    }
0
source

Yes, but it’s not so trivial to explain how to do it. First understand why you ran into this problem:

$value = $a->b->c;

This will first return NULLfor $a->b. So you wrote:

$value = NULL->c;

, NULL NULL - ( namne it NULLObect), NULL.

, NULL PHP, PHP , .

NULLObect, .

class property 
{

    public function __get($name)
    {
        isset($this->$name) || $this->$name = new NULLObject();
        return  $this->$name;
    }
}


class NULLObject extends Property {};

, , . , , PHP- , . , , PHP.

:

+1

Yes, I know that it was 4 years ago, but this week I had a similar problem, and trying to solve it, I found this topic. So here is my solution:

class NoneWrapper
{
    private $data;

    public function __construct($object)
    {
        $this->data = $object;
    }

    public function __get(string $name)
    {
        return property_exists($this->data, $name)
            ? new NoneWrapper($this->data->$name)
            : new None;
    }

    public function __call($name, $arguments)
    {
        if (is_object($this->data)) {
            return (property_exists($this->data, $name))
            ? $this->data->$name
            : null;
        } else {
            return null;
        }
    }

    public function __invoke()
    {
        return $this->data;
    }
}

class None
{
    public function __get(string $name) {
        return new None;
    }

    public function __call($name, $arguments)
    {
        return null;
    }

    public function __invoke()
    {
        return null;      
    }
}

$object = new NoneWrapper(
    json_decode(
        json_encode([
            'foo' => [
                'bar' => [
                    'first' => 1,
                    'second' => 2,
                    'third' => 3,
                    'fourth' => 4,
                ],
            ]
        ])
    )
);

var_dump($object->foo->bar->baz()); // null
var_dump($object->foo->bar->third()); // 3
0
source

All Articles