PHP OOP性能:参数还是实例变量? [英] PHP OOP Performance : Parameter or Instance Variable?

查看:83
本文介绍了PHP OOP性能:参数还是实例变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想获得OOP问题的答案,但到目前为止找不到任何信息.

Im curious about getting an answer for a OOP question but can't find any info so far.

在这里,编写类和方法是为每个方法传递参数或使用实例/字段变量和$ this-> x更快;

Here goes, writing classes and methods is it faster to pass in parameters for each method or use instance/field variables and $this->x;

在运行时哪个会更快?

class ExampleByParameter(){

 function SomeMethod($a,$b){
 echo $a." ".$b;
 return;
 }

}

class ExampleByInstance(){

 function __construct($a,$b){
 $this->a=$a;
 $this->b=$b;
 }

 function SomeMehtod(){
 $a=$this->a;
 $b=$this->b;
 echo $a." ".$b;
 return;
 }

}

我想他们与上面的示例没有什么区别,但是我认为使用更复杂的代码可能会有很大的区别.

I would imagine their would be no difference with the examples above but im thinking there might be a significant difference with more complicated code.

推荐答案

性能差异将可忽略不计.您应该真正确定哪种模式最适合您的类结构.

The performance differece is going to be negligible. You should really be deciding which pattern is best for your class structure.

您的课程是否依赖于$a$b才能正常运行?将它们放在构造函数中.

Does your class depend on $a and $b to operate properly? Put them in the constructor.

SomeMethod是公开的吗?除了返回值以外,它对提供的变量是否有任何作用?如果是这样,请将其保留为参数.

Is SomeMethod public? Does it do anything with the supplied variables beyond what it returns? If so, keep them as parameters.

在大型项目中,准确的类设计与可忽略的性能改进相比,您将获得更多的好处.

In a large-scale project, you will gain more benefit from accurate class design vs negligible performance improvements.

这篇关于PHP OOP性能:参数还是实例变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆