php反射调用失败

hwjfshx hwjfshx 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
<?php
/**
* A counter class
*/
class Counter{
private $ss = 'dd';
private $dd = 'ss';
public $o = 'o';

public function getSs(){
return $this -> ss;
}
public function getO(){
return $this -> o;
}
public function getDd(){
return $this -> dd;
}
}

$ct = new Counter();
echo invoke($ct, getMethod('ss'));
function getMethod($fieldName){
return 'get' . ucfirst($fieldName);
}
function invoke($obj, $methodName){
$reflector = new ReflectionObject($obj);
$method = $reflector -> getMethod($methodName);
return $method -> invoke(null); //反射调用有问题。。。
}
?>
直接echo $method有内容
但最后一句$method -> invoke(null)运行时就出错了
Fatal error: Uncaught exception 'ReflectionException' with message 'Non-object passed to Invoke()' in C:\Program Files\AppServ\www\guestbook\index.php:54 Stack trace: #0 C:\Program Files\AppServ\www\guestbook\index.php(54): ReflectionMethod->invoke(NULL) #1 C:\Program Files\AppServ\www\guestbook\index.php(47): invoke(Object(Counter), 'getSs') #2 {main} thrown in C:\Program Files\AppServ\www\guestbook\index.php on line 54

我传进去的是已经实例化的对象。参照PHP文档的调用方法写的。
网上搜了很久,没有搜到答案。
请高人指点
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
hwjfshx
基础编程 21,889 成员 140,329 内容