如何修复错误 从空值创建默认对象.


how fix an error Creating default object from empty value

我有一个脚本,在一个类($slug)中带有变量。

当我运行此脚本时,它可以工作,但我收到警告:警告:从空值创建默认对象。我知道,我可以在我的 php.ini 中隐藏此警告,但是 ik 如何修复此警告?

    $test = new myclass();
class myclass {
    private $order;
    public function __construct() {
        $this->order = (object) NULL;
    }
    public function setVars($slug, $number) {
        $this -> order -> $slug -> number = $number;
    }
    public function getVars($slug) {
        echo $this -> order -> $slug -> number;
    }
}
$test -> setVars($slug="hello", $number=5);
$test -> getVars($slug="hello");

只需将订单对象定义为 stdClass

public function __construct() {
    $this->order = new stdClass();
}

看到这个: 从 PHP 中的空值创建默认对象?

还有这个:http://www.php.net/manual/en/language.oop5.basic.php