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

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>';
    
    点赞 评论

相关推荐 更多相似问题