20,397
社区成员




提供一个字符串,怎么把这个字符串生成一个类的方法。主要是要让method_exists检测返回存在。
$str = "wjy";
//createClass();
function createClass(){
global $str;
$class = <<<WJY
class {$str}{
function {$str}(){
echo "hello world";
}
}
WJY;
eval($class);
$GLOBALS[$str] = new $str();
}
createClass();
// 检查类是否存在该方法
if (method_exists($str, $str)) {
echo "类 {$str} 存在方法 {$str}。\n";
} else {
echo "类 {$str} 不存在方法 {$str}。\n";
}