douzhong3038 2018-11-06 08:47
浏览 80
已采纳

PHP - ReflectionFunction - 函数Test :: test_function()不存在

I could use RefexionFunction outside of a class, but inside a class I get an exception.

Fatal error: Uncaught ReflectionException: Function Test::test_function() does not exist in test.php.

<?php

function parameters($functionName,$args){
    $f = new ReflectionFunction($functionName);
    ....
}


class Test{
    public function test_functionA($abc,$d,$e,$f) {
        parameters(__METHOD__,func_get_args()); 
    }

    protected function test_functionB($abc,$d,$e,$f) {
        parameters(__METHOD__,func_get_args()); 
    }

    private function test_functionC($abc,$d,$e,$f) {
        parameters(__METHOD__,func_get_args()); 
    }
}

$test = new Test();
$test->test_function('something',1,2,array(123,456));

?>

Appreciate your help.

  • 写回答

1条回答 默认 最新

  • doukuang6795 2018-11-06 09:01
    关注

    Your error:

    Fatal error: Uncaught ReflectionException: Function Test::test_function() does not exist in test.php.

    Doesn't refer to the function name quite as you expect it to.

    ReflectionClass docs says this:

    The ReflectionClass class reports information about a class.
    ref: https://secure.php.net/manual/en/class.reflectionclass.php

    You want to use a combination of methods available in that class to get information about the passed method like this:

    public function parameters($class, $fnc)
    {
        $f = new ReflectionClass($class);
    
        if ($f->hasMethod($fnc)) {
            return 'howdy folks';
        } else {
            return 'not so howdy folks';
        }
    }
    

    You first pass the class before checking if the function exists. You can then use the built-in function hasMethod to check if the function exists. You then use the parameters function like this:

    public function testFunction()
    {
        return $this->helper->parameters(__CLASS__, __FUNCTION__);
    }
    

    All together the code looks like this:

    <?php
        ini_set('display_startup_errors', 1);
        ini_set('display_errors', 1);
        error_reporting(-1);
    
        class paramsHelper
        {
            public function parameters($class, $fnc)
            {
                $f = new ReflectionClass($class);
                $f->getMethod($fnc);
    
                if ($f->hasMethod($fnc)) {
                    return 'howdy folks';
                } else {
                    return 'not so howdy folks';
                }
    
                return $f;
            }
        }
    
        class Test
        {
            protected $helper;
    
            public function __construct($helper)
            {
                $this->helper = $helper;
            }
    
            public function testFunction()
            {
                return $this->helper->parameters(__CLASS__, __FUNCTION__);
            }
        }
    
        $test = new Test(new paramsHelper());
    
        echo '<pre>';
        print_r($test->testFunction());
        echo '</pre>';
    

    One of your other problems is that __METHOD__ actually returns a string like this: Test::testFunction not testFunction - hence my use of __FUNCTION__ instead.

    Edit:

    To get the parameters of the passed method, change your parameters method to this:

    class paramsHelper
    {
        public function getMethodParameters($class, $fnc)
        {
            $f = new ReflectionMethod($class, $fnc);
    
            echo '<pre>';
            print_r($f->getParameters());
            echo '</pre>';
        }
    }
    

    This uses ReflectionMethod in place of ReflectionClass - this is more inline with your intended use.

    ref: https://secure.php.net/manual/en/class.reflectionmethod.php

    use:

    class paramsHelper
    {
        public function getMethodParameters($class, $fnc)
        {
            $f = new ReflectionMethod($class, $fnc);
    
            echo '<pre>';
            print_r($f->getParameters());
            echo '</pre>';
        }
    }
    
    class Test
    {
        protected $helper;
    
        public function __construct($helper)
        {
            $this->helper = $helper;
        }
    
        public function testFunction($a = '', $b = 1, $c = 3)
        {
            return $this->helper->parameters(__CLASS__, __FUNCTION__);
        }
    }
    
    $test = new Test(new paramsHelper());
    
    echo '<pre>';
    print_r($test->testFunction());
    echo '</pre>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示