考虑这个类:
class Test {
public $obj;
function __construct ($obj) {
$this->obj = $obj;
} $obj有自己的公共函数。我通过我的代码将它称为$t = new Test($obj); $t->obj->do();
我希望允许$obj为空,而不会触发错误。有没有可能用PHP的魔法函数在没有显式设置函数的情况下总是返回false ?还有,PHP < 5.3有解决方案吗?
发布于 2010-12-18 23:17:48
我现在不能测试它,但这可能会起作用:
class default {
public function __call($name, $arguments) {
return false;
}
}
class Test {
public $obj;
public function __construct($obj = NULL) {
if($obj === NULL) $this->obj = new default;
else $this->obj = $obj
}
}https://stackoverflow.com/questions/4478545
复制相似问题