Nate
Nate

Reputation: 114

ReflectionMethod's get type is retuning an empty object

So, I am trying the get the types of the methods, to instantiate the classes, for example:

I have a class called mycontroller and a simple method called page which has a class Type hint, for example:

class MyController
{
    public function page(AnotherClass $class)
    {
        $class->intro;
    }
}

I also have another class, litterly called anotherclass (very original, I know)

class AnotherClass
{
    public $intro = "Hello";
}

Okay, so that's the basics, now I am trying to get the type of MYControllers method arguments page: anotherclass

You can see the logic of my code below:

Class Route
{
    /**
     * Method paramaters
     *
     * @var array
     */
    protected $params;

    /**
     * The class and method
     *
     * @var array
     */
    protected $action;

    /**
     * Get the paramaters of a callable function
     *
     * @return void
     */
    public function getParams()
    {
       $this->params = (new ReflectionMethod($this->action[0], $this->action[1]))->getParameters();
    }

    /**
     * Seperate the class and method
     *
     * @param [type] $action
     * @return void
     */
    public function getClassAndMethod($action = null)
    {
        $this->action = explode("@", $action);

    }

    /**
     * A get request
     *
     * @param string $route
     * @return self
     */
    public function get($route = null)
    {   
        if(is_null($route)) {
            throw new Exception("the [$route] must be defined");
        }

        return $this;
    }

    public function uses($action = null)
    {
        if(is_null($action)){
            throw new Exception("the [$action] must be set");
        }  

        if(is_callable($action)){
            return call_user_func($action);
        }

        // Get the action
        $this->getClassAndMethod($action);

        // Get the params of the method
        $this->getParams();

        foreach ($this->params as $param) {

            print_R($param->getType());
        }

        // var_dump($action[0]);
    }
}

Which is simply being called like so:

echo (new Route)->get('hello')->uses('MyController@page');

So, what the above does, is it splits the uses method paramater via the @ sign, the [0] will be the class and the [1] will be the class's method, then I am simply ReflectionMethod to get the parameters of said method, and then I am trying to get the parameter type, which, is what I am stuck on, because it just keeps returning an empty object:

ReflectionNamedType Object { )

So, my question is, why is it returning an empty object, and how can I get the type of the parameter?

Upvotes: 1

Views: 511

Answers (1)

Syscall
Syscall

Reputation: 19780

You have to echo instead of print_r :

foreach ($this->params as $param) {
    echo $param->getType() ; //AnotherClass
}

Because ReflectionType use __toString() to display it.

Or

foreach ($this->params as $param) {
    print_r($param->getClass()) ; //AnotherClass
}

Upvotes: 2

Related Questions