<?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')); //输出dd
function getMethod($fieldName){ 
return 'get' . ucfirst($fieldName); 

function invoke($obj, $methodName){ 
/*$reflector = new ReflectionObject($obj); 
        $method = $reflector -> getMethod($methodName); 
        return $method -> invoke(null); //反射调用有问题 */

return $obj ->$methodName(null);

?>

解决方案 »

  1.   


    <?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')); //输出dd
    function getMethod($fieldName){ 
        return 'get' . ucfirst($fieldName); 

    function invoke($obj, $methodName){ 
        /*$reflector = new ReflectionObject($obj); 
        $method = $reflector -> getMethod($methodName); 
        return $method -> invoke(null); //反射调用有问题 */
        return $obj ->$methodName(null);

    ?>
      

  2.   

    非常感谢
    原来是类似JS的函数指针来用不过,PHP参考手册上能够用ReflectionMethod的invoke方法来调用
    还是不理解。手册上传的用的是ReflectionClass,我用ReflectionObject而已