警告:尝试分配非对象的属性,但是对象存在 - php

这是我的课:

class Network {
    protected $value = null;
    protected $properties = array();
    public $type = null;

    function __construct($value = null, $type = null) {
        $this->value = $value;
        $this->type = $type;
    }

    public function __set($name, $value) {
        if(isset($this->{$name})) {
            $this->{$name} = $value;
        } else {
            $this->properties[$name] = new self($value, null);
        }
    }

    public function __get($name) {
        return $this->properties[$name]->value;
    }

    public function __toString() {
        return $this->value;
    }
}

这就是我想要做的:

$networks = new \stdClass();
$networks->test= new \Orange_Post\Network('Test');
$networks->test->enabled = true;
$networks->test->enabled->type = 'boolean';

但是我得到了错误:

警告:尝试在最后一行分配非对象的属性,$ networks-> test-> enabled-> type ='boolean';

这是我第一次尝试扩展并执行类似的操作,但是我无法弄清楚自己在做什么。

参考方案

那么这是怎么回事?

$networks = new \stdClass();
$networks->test= new \Orange_Post\Network('Test');
$networks->test->enabled = true;
$networks->test->enabled->type = 'boolean';
 ↑         ↑     ↑        ↑ Tries to access a property of a value ('TRUE')
 |         |     | 'enabled' as property never exists     
 |         | Is a property with an instance of '\Orange_Post\Network'
 | Is an instance of '\stdClass'

首先,当您尝试将true分配给属性enabled时。然后__set()被调用,因为该属性不存在。

public function __set($name, $value) {
    if(isset($this->{$name})) {
        $this->{$name} = $value;
    } else {
        $this->properties[$name] = new self($value, null);
    }
}

在该魔术方法中,您将类本身的新实例分配给属性数组properties,并使用名称(enabled)作为索引。

$networks->test->enabled->type = 'boolean';

之后,您尝试将属性type设置为属性enabled。在这里__get被调用。

public function __get($name) {
    return $this->properties[$name]->value;
}

因此,现在您只需返回properties的数组值并返回其属性value。而且属性value不是对象。只需删除->value部分,您的代码将返回该对象。

换句话说,您的最后一行:

$networks->test->enabled->type = 'boolean';

成为:

$networks->test->properties["enabled"]->value->type = 'boolean';
                 ↑                      ↑ This is causing the problem
                 | Holds an object      | Holds 'true' as property value

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…

PHP数组可以这样做吗? - php

可以说;我有一个$ friends数组,其中有2,000个不同的friendID号+我有一个带有10,000 bulletinID号的$ bulletins数组,该$ bulletins数组还将具有另一个值,该ID的用户ID是发布公告条目的用户现在可以获取所有具有与FriendsID数组中的userID匹配的userID的bulletinID号吗?甚至有可能…

PHP:检查谁看过发送的电子邮件? - php

我正在向某些用户发送电子邮件,并且想知道是谁阅读的,这意味着如果有人阅读了该电子邮件,则将维护一个日志文件,其中包含该用户的电子邮件地址以及日期/时间/ IP。为此,我发送一个带有电子邮件(html模板)的javascript函数,当用户打开该电子邮件时,它仅会警告用户的电子邮件地址,例如:for($n=0; $n<sizeof($checkBox);…

CakePHP将数据传递到元素 - php

我的控制器中有以下代码:function index() { $posts = $this->set('posts', $this->Portfolio->find('all')); if (isset($this->params['requested'])) { retur…

phpWord中的粗体,空格和缩进文本 - php

我有一些要加粗的文本,与前几段和后几段分开并缩进。我不能让所有三个属性一起工作。这适用于粗体和空格:$section = $phpWord->addSection(); $section->addText( 'Re: Your Application for Post of Security Guard', array(�…