【问题标题】:How to use __get() to return null in multilevel object property accessing?如何使用 __get() 在多级对象属性访问中返回 null?
【发布时间】:2012-05-05 13:16:28
【问题描述】:

如何使用 __get() 在多级对象属性中返回 null 访问下面这样的情况?

例如,这是我的课,

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's 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;

    }
}

我如何使用它,

$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);

结果,

null

但当输入数组为null 时,我收到一条错误消息,显示为 NULL,

$type = null;
$type = $object->array_to_object($type,true);
var_dump($type->a->cc);

结果,

Notice: Trying to get property of non-object in C:\wamp\www\test...p on line 68
NULL

这种情况下可以返回NULL吗?

【问题讨论】:

  • PHP 语言不知道多级对象,对象始终是单个节点,在上下文中访问时不知道它是升序还是降序。

标签: php php-5.3 stdclass


【解决方案1】:

是的,但要解释如何做到这一点并不是那么简单。首先了解您遇到该问题的原因:

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

这将首先为$a->b 返回NULL。所以实际上你写道:

$value = NULL->c;

因此,您需要在未设置的项目上返回 NULL-object(让我们将其命名为 NULLObect),而不是 NULL,它与您的基类类似并表示 NULL

如你所想,你无法在 PHP 中真正用它来模拟 NULL,而且 PHP 的语言功能太有限,无法无缝地做到这一点。

但是你可以尝试接近我描述的NULLObect

class property 
{

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


class NULLObject extends Property {};

请注意,这可能不是您要查找的内容。如果它不符合您的需求,那么 highjly 很可能 PHP 是您用于编程的错误语言。使用一些比 PHP 具有更好的成员覆盖特性的语言。

相关问题:

【讨论】:

  • 感谢您的解释。我可以问如何在上面的脚本中创建NULLObect 吗?谢谢。
  • 我不确定它是否真的对你有意义,但我已经用一个简单的代码示例扩展了答案。
【解决方案2】:

您可以返回新属性而不是 null

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

【讨论】:

    【解决方案3】:

    是的,我知道已经是 4 年前了,但本周我遇到了类似的问题,在我试图解决它时,我发现了这个帖子。所以,这是我的解决方案:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 2014-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多