PHP can I call unexistent function with call_user_func

空扰寡人 提交于 2020-01-05 03:37:11

问题


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()};


来源:https://stackoverflow.com/questions/14923235/php-can-i-call-unexistent-function-with-call-user-func

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!