PHP Reflection - Get Method Parameter Type As String

后端 未结 6 1873
無奈伤痛
無奈伤痛 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:24

    I supposed this is what you are looking for:

    class MyClass {
    
        function __construct(AnotherClass $requiredParameter, YetAnotherClass $optionalParameter = null) {
        }
    
    }
    
    $reflector = new ReflectionClass("MyClass");
    
    foreach ($reflector->getConstructor()->getParameters() as $param) {
        // param name
        $param->name;
    
        // param type hint (or null, if not specified).
        $param->getClass()->name;
    
        // finds out if the param is required or optional
        $param->isOptional();
    }
    

提交回复
热议问题