Automatic functions in object 's object?
Consider this class:
class Test {
public $obj;
function __construct ($obj) {
$this->obj = $obj;
}
$obj has its own public functions. I call it through my code as $t = new Test($obj); $t->obj->do();
I want to allow $obj to be empty, without triggering errors. Is it possible to perform some trick with PHP 's magic开发者_如何学编程 functions to always return false if the function is not explicitly set? Also, would there be a solution for PHP < 5.3?
I cannot test this right now but this could work:
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
}
}
精彩评论