PHP Reflection - Get Method Parameter Type As String

后端 未结 6 1861
無奈伤痛
無奈伤痛 2020-12-01 18:44

I\'m trying to use PHP reflection to dynamically load the class files of models automatically based upon the type of parameter that is in the controller method. Here\'s an e

6条回答
  •  离开以前
    2020-12-01 19:15

    getType method can be used since PHP 7.0.

    class Foo {}
    class Bar {}
    
    class MyClass
    {
        public function baz(Foo $foo, Bar $bar) {}
    }
    
    $class = new ReflectionClass('MyClass');
    $method = $class->getMethod('baz');
    $params = $method->getParameters();
    
    var_dump(
        'Foo' === (string) $params[0]->getType()
    );
    

提交回复
热议问题