duanjue7508 2010-05-29 22:12
浏览 93
已采纳

获取类方法的参数列表

What I'd like is for this class

class Car {
    public function __construct(Engine $engine, Make $make, Model $model)
    {
          // stuff
    }
}

Get an array that has the types needed to construct this car (Engine, Make, Model) in the order they are needed for the constructor. I'm using this for a Dependency Injection-esque thing I'm making.

Update: I made these classes to accomplish the task but I feel like there is still a better way:

    <?php
    class Engine{}
    class Make{}
    class Model{}
    class Car {
        public function __construct(Engine $engine, Make $make, Model $model,
        $foo,
        $cats = 'dogs',
        Engine $anotherEngine = NULL)
        {
              // stuff
        }
    }
    class ServiceParameter extends  ReflectionParameter
    {
            public $type = NULL;

            public function __construct()
            {
            $args = func_get_args();
            call_user_func_array(array('parent', '__construct'), $args);
                    $str = parent::__toString();
                    $str = explode(" ", $str);
                    $str = $str[4];
                    if(substr($str, 0, 1) != '$')
                            $this->type = $str;
            }

    }
    class ServiceMethod extends ReflectionMethod
    {
        private $_parameter_cache = NULL;
        private function _wrapParameter(&$item, $key)
        {
            $item = new ServiceParameter(array($item->getDeclaringClass()->name,
                 $item->getDeclaringFunction()->name),
                $item->name);

        }
        public function getParameters()
        {
            if(is_array($this->_parameter_cache))
                return $this->_parameter_cache;
            else
            {
                $rms  = parent::getParameters();
                array_walk($rms, array($this, '_wrapParameter'));
                $params = $rms;
                $this->_parameter_cache = $params;
                return $params;

            }
        }
    }


    $c = new ServiceMethod("Car", "__construct");
    $args = $c->getParameters();
    foreach($args as $arg)
    {

        echo "Parameter Name: {$arg->name}
";
        echo "GetName: {$arg->getName()}
";
        echo "Type: {$arg->type}
";


    echo "

";
}

Which outputs:

Parameter Name: engine
GetName: engine
Type: Engine


Parameter Name: make
GetName: make
Type: Make


Parameter Name: model
GetName: model
Type: Model


Parameter Name: foo
GetName: foo
Type: 


Parameter Name: cats
GetName: cats
Type: 


Parameter Name: anotherEngine
GetName: anotherEngine
Type: Engine

I know there's some redundant stuff ($params = $rms) but that's a result of rushing and changing the code around a lot to get it to work. I feel like constructing a whole new ServiceParameter when we already have the ReflectionParameter instance is a waste. Is there anyway around this ($item = (ServiceParameter)$item didn't work)

  • 写回答

2条回答 默认 最新

  • dqlm80253 2010-05-30 00:10
    关注
    class Engine { }
    class Make { }
    class Model { }
    class Car {
      public function __construct(array $a, $b, Engine $engine, Make $make, Model $model)
      {
        // stuff
      }
    }
    
    
    $rc = new ReflectionClass('Car');
    $ctor = $rc->getConstructor();
    foreach( $ctor->getParameters() as $rp ) {
      $cn = $rp->getClass();
      if ( $cn instanceof ReflectionClass ) {
        $cn = $cn->getName();
      }
    
      echo $cn, ' ', $rp->getName(), "
    ";
    }
    

    prints

     a
     b
    Engine engine
    Make make
    Model model
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分