0
call_user_funcで登録されたメソッドに対してmethod_existsを呼び出してみます。method_existsで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 falseを返します。 method_existsでこのメソッドをチェックするには? method_existsがfalseを返す理由
そしてこの匿名関数を確認する方法? – stdex
私の答えを更新しました。 –
いいえ、動作しません。例:is_callable([$ obj、 "test_test"]) - trueを返しますが、falseを返します。 – stdex