dongming8867 2014-07-08 14:16
浏览 87
已采纳

PHP:性能:splat运算符或反射

In an app that I'm creating, I need to pass an unknown number of parameters to an unknown constructor of a class. The class (+ namespace) is a string, that is in $class. The parameters are in an array.

This application will be deployed over a couple of months, so we thought we could develop it already in PHP 5.6. So I thought that the solution of this would be:

$instance = new $class(...$args);

This is working...

But my collegues don't want to accept this, because the CI server does not understand this line of code. Their solution would rather be:

$reflect = new \ReflectionClass($class);
$instance = $reflect->newInstanceArgs($args)

Now: both are working fine, so that's not the problem. But my thought is that Reflection is slower then using other ways (like the PHP 5.6 splat operator).

Also the question: is reflection a good way, of should I use the splat operator from the moment the CI server does understand that line?

  • 写回答

2条回答 默认 最新

  • duanqi5333 2014-07-08 14:27
    关注

    Definitely go for the splat operator, why? It's much faster than the reflection approach (I'm using it and the implementation seems to be very good). Also reflection breaks just about anything that has to do with design, it allows you to break encapsulation for instance.

    PS: Isn't it $instance = new $class(...$args);?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器