How to check if a function is public or protected in PHP

后端 未结 3 1741
天命终不由人
天命终不由人 2020-12-15 16:32

I am building an API where the user requests a \'command\', which is passed into a class. Assuming the command matches a PUBLIC function, it will execute successfully. If th

3条回答
  •  孤城傲影
    2020-12-15 16:54

    Simply use ReflectionMethod:

    /**
     * Look for Command method
     */
    if (method_exists($this, $sMethod))
    {
        $reflection = new ReflectionMethod($this, $sMethod);
        if (!$reflection->isPublic()) {
            throw new RuntimeException("The called method is not public.");
        }
        /**
         * Run the command
         */
        return $this->$sMethod($aParameters);
    }
    

提交回复
热议问题