php - Using call_user_func with method_exists -


try call method_exists on method registred call_user_func.

<?php   class stdclass1 {     public static $methods = [];      public function __call($method, $arguments) {         return call_user_func_array(closure::bind(self::$methods[$method], $this, get_called_class()), $arguments);     }      public function __set($name, $value) {         if (is_callable($value)) {             self::$methods[$name] = $value;         } else {             parent::__set($name, $value);         }     }  }  class stdclass2 {     function stdrunmethod()     {         $obj = new stdclass1();         $obj->test = function () {             echo 'a simple function'.php_eol;         };         var_dump(method_exists($obj, "test"));     } } $obj = new stdclass2(); $obj->stdrunmethod(); 

method_exists return false. how check method method_exists? why method_exists return false?

because test not method. it's property stores anonymous function.

if want check if value of property can called function can use is_callable:

var_dump(is_callable([$obj, "test"])); 

Comments