开发者

Which way is better to call a function static way or object way ?

开发者 https://www.devze.com 2023-01-18 06:31 出处:网络
Hi I\'m new to OOPS, Can you guys suggest me which way is better to call a function of a cl开发者_开发问答ass in PHP and why? the scenario is as follows.

Hi

I'm new to OOPS, Can you guys suggest me which way is better to call a function of a cl开发者_开发问答ass in PHP and why? the scenario is as follows.

class A
{
     function B (){}
}

Which method will take less resource to call the function B

1. A::B();

or

2. $obj = new A();  
   $obj->B();


Well, the second way creates an unnecessary temporary object, so the first one is faster. But if you have an object created anyway, it wouldn't matter.


Firstly in PHP 7 calling non-static methods statically is deprecated so you will get an error. Secondly it is better to call the method object way so code will be backward compatible.

0

精彩评论

暂无评论...
验证码 换一张
取 消