PHP can I call unexistent function with call_user_

2019-08-07 02:10发布

问题:

Here it what I want to do: I have an object

class TestObject {
      public function __call($name,$args){
        if($name == 'somemethod'){
          print "Yesssss!!!!!!";
        }
      }
}

than in code i'm calling this method like this:

$obj = new TestObject()
$obj->somemethod()  // should work according to http://www.php.net/manual/en/language.oop5.overloading.php#object.call
call_user_func(array('myobject','somemethod')) // this does not work!!!

Is there any way to get the last example to work?

回答1:

You can use

call_user_func(array($obj,'somemethod'));

or

$method = 'somemethod';
$obj->$method();


回答2:

$obj_name = 'myobject';
$method_name = 'somemethod';
${$obj_name}->${$method->name()};