Question

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?

Was it helpful?

Solution

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
      }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top