PHP 在不调用构造函数的情况下创建对象
可以利用PHP的反射功能ReflectionClass :: newInstanceWithoutConstructor,实现这一需求。
例子:
<?php
class a
{
public $foo=0;
public $bar=9;
public function __construct()
{
$this->foo=1;
echo "调用构造函数\n";
}
}
$ref = new ReflectionClass('a');
$inst = $ref->newInstanceWithoutConstructor();
print_r($inst);
以上输出:
a Object
(
[foo] => 0
[bar] => 9
)
相关博文
PHP 在不调用构造函数的情况下创建对象