php反射调用失败
<?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文档的调用方法写的。
网上搜了很久,没有搜到答案。
请高人指点
/**
* 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文档的调用方法写的。
网上搜了很久,没有搜到答案。
请高人指点
...全文
相关推荐
php反射api.pdf
PHP ReflectionClass Api
php如何实现不借助IDE快速定位行数或者方法定义的文件和位置
主要介绍了php如何实现不借助IDE快速定位行数或者方法定义的文件和位置的相关资料,需要的朋友可以参考下
PHP单元测试利器 PHPUNIT深入用法(二)第1/2页
1、markTestSkipped和markTestIncomplete 在phpunit中,有两个有用的方法markTestSkipped和markTestIncomplete。它们能允许你编写的单元测试中不单是只有通过和失败两种结果。markTestSkipped能让PHPUNIT不去执行某个已经编写好的测试方法。举个例子说明,比如下面的程序: #div_code img{border:0px;} <?phppublic function testThisMightHaveADb(){ $myObject->createObject(); try { $db = new Da
DSC大商创多商户电商系统完整部署教程(附PHP7.4/PHP8兼容修复方案)
丰富的营销工具:秒杀、团购、优惠券一应俱全 多商户入驻:商家可以自主管理店铺 多端支持:PC+H5+小程序+APP全端覆盖 完整的佣金结算:平台与商家分账
PHP反射介绍
PHP,是英文超级文本预处理语言Hypertext Preprocessor的缩写。PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,被广泛的运用。 该文档是介绍PHP反射机制的。


基础编程
