检查ia函数存在时遇到问题 - 我知道function_exists()和method_exists()但是无法获得我寻找的功能
我通过套接字连接传递函数,如$ MM-> Player-> Play().之后我评估它们并通过套接字连接返回结果.
我做过滤功能(必须从$ MM开始,其他任何东西都将被拒绝),但我无法获取我的代码来检查函数是否存在.这很重要,因为如果函数不存在,服务器将崩溃.
任何人都知道这个解决方案?
提前致谢.
代码:
$MM = new SDBApplication;
在SDBApplication的构造函数中:
$this->Player = new SDBPlayer;
我尝试了以下方法来检查:代码:
method_exists($MM, "Player::Play");
function_exist("$MM->Player->Play);
即使函数存在,它们都返回false
解决方案
$parts = explode('->', $string);
$numParts = count($parts)-1;
$object = '$MM';
for($i=0; $i < $numParts; $i++){
$object .= '->'.$parts[$i];
}
$parts[$numParts+1] = preg_replace('(\\(.*\\))', '', $parts[$numParts]);
eval( '$check = method_exists('.$object.', '.$parts[$numParts+1].');');
Run Code Online (Sandbox Code Playgroud)
它需要进行评估,因为输入是一个字符串.
在你的构造函数中以这种方式尝试 SDBApplication
method_exists($this->Player, 'Play');
Run Code Online (Sandbox Code Playgroud)