【问题标题】:PHP - E_STRICT: Creating default object from empty valuePHP - E_STRICT:从空值创建默认对象
【发布时间】:2012-12-27 15:14:31
【问题描述】:

我看到Creating default object from empty value 这里有大约一百个不同的问题。他们似乎都没有真正帮助解决我的问题。

我在方法中分配了同一类的子对象。这会触发Creating default object from empty value 错误。

class myClass {


    function __construct($rootName, $allowHTML = false, $endTag = true) {
        $this->rootElement = $rootName;
        $this->elements = array();
        $this->attributes = array();
        $this->value = "";
        $this->allowHTML = $allowHTML;
        $this->endTag = $endTag;
        $this->styles = array();
        $this->childID = 0;
    }

    // ... OTHER METHODS HERE (ALL PROPERTIES DECLARED)

    function assignElement(&$theElement) {
        // Get the index.
        $index = count($this->elements);

        // Assign the element.
        $this->elements[$index] = $theElement;

        if (get_class($theElement) == get_class($this)) {
            $this->elements[$index]->childID = $index;
        }

        // Return the node.
        return $this->elements[$index];
    }
}

错误发生在$this->elements[$index]->childID = $index;。我该如何正确处理?

【问题讨论】:

    标签: php strict


    【解决方案1】:

    似乎您正在将NULL 传递给assignElementget_class 不带参数或 null 作为参数调用,返回定义它的对象的类,因此您的 if 条件对于 null 值是正确的。你应该先使用is_object

    【讨论】:

    • 这就是正在发生的事情。这在大型应用程序中被广泛使用,并且传递给它的变量有时是未定义的。有趣的是它给了我一个 E_STRICT 错误,而不是一个 NOTICE / undefined 错误。
    • Lack of NOTICE 可能是因为您的方法中的引用。
    猜你喜欢
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2012-02-12
    • 2023-03-09
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    相关资源
    最近更新 更多