在类属性中分配空对象?

时间:2014-05-27 02:08:25

标签: php class syntax properties

将占位符(空对象)分配给类属性的正确方法是什么?

示例:

class something {

    public $my_object = object; // ???

    public function __construct() {

        //create some object, via some function...

        $this->my_object = $some_object;
    }

}

1 个答案:

答案 0 :(得分:3)

这样的事情可能是:

class something {

    public $my_object;

    public function __construct() {

        $some_object = new StdClass();

        $this->my_object = $some_object;
    }

}

测试:

$obj = new something();
var_dump($obj);

输出:

object(something)#1 (1) {
  ["my_object"]=>
  object(stdClass)#2 (0) {
  }
}