【问题标题】:Is there any way to find argument type list of a function in php?有没有办法在php中找到函数的参数类型列表?
【发布时间】:2022-01-21 11:01:40
【问题描述】:

例如,看看这个 php 代码:

class AuthController extends Controller {
   public function store(LoginRequest $request, int $id) {
      // code...
   }
}

我想获取方法 AuthController::store() 的参数类型列表。

例如,这样做:

print_r(get_arg_types('AuthController::store'));

应该返回:

Array (
   [0] => LoginRequest
   [1] => int
)

现在,这个get_arg_types() 还不存在,但我想创建并使用它。有办法吗?

【问题讨论】:

    标签: php function types arguments


    【解决方案1】:

    是的,你要找的就是Reflection,而 php 有一系列的函数来做这些事情。

    这很可能会让您上路:ReflectionMethod。 这里是some more generic background info

    【讨论】:

      【解决方案2】:

      首先反映类,然后是方法,然后是parameters

      <?php
      
      class Controller {}
      
      class AuthController extends Controller {
         public function store(LoginRequest $request, int $id) {
            // code...
         }
      }
      
      $ref = new ReflectionClass('AuthController');
      $methods = $ref->getMethods();
      $params = $methods[0]->getParameters();
      foreach ($params as $param)
      var_dump($param->getType()->getName());
      

      输出

      字符串(12)“登录请求” 字符串(3)“整数”

      Teh Playground!

      【讨论】:

        猜你喜欢
        • 2013-01-18
        • 2013-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-17
        • 1970-01-01
        • 1970-01-01
        • 2018-08-19
        相关资源
        最近更新 更多