开发者

Automatic functions in object 's object?

开发者 https://www.devze.com 2023-01-31 01:12 出处:网络
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-&

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
      }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消