在函数参数中根据对象大小传递对象是否有性能差异?


Is there any performance difference when passing objects in function parameter based on object size?

我有A, B, C, D类

class A {
   public $b;
   public $c;
   public $d;
   // Other properties
   function __construct() {
       $this->b = new B();
       $this->c = new C();
       $this->d = new D();
    }
   function process() {
       $x = new ExternalClass($this->b, $this->c, $this->d)
       // $x = new ExternalClass($this)
   }
   // other functions. 
 }

在这里,传递参数为$this而不是$this->b, $this->c, $this->d是否有任何性能差异?之后,我可能需要发送更多的对象$this->e, $this->f等。而不是传递每个对象变量,如果我传递$this对象,我可以访问任何我想要的对象。但是我想知道这是否涉及到性能问题。

在这种情况下,您真的不需要担心性能。在现实世界的应用中,你永远无法衡量差异。

我更担心的是你的执行。阅读一些关于依赖注入(DI)的知识,并尝试使用单一责任原则(SRP)将应用程序解耦到更多的类。