开发者

Is there any overhead of using a PHP Class Object vs a Static Class Method?

I am wanting to know if there is any extra overhead of using an Object in PHP instead of using a static method based on my examples below?

Sesseion object from Session class

$session = new Session;
$session->set(user_id, $uswer_id); //set session var
$session->get(user_id);            // get session var

开发者_Go百科

VS


Static methods from Session class

Session::set(user_id, $uswer_id); //set session var
Session::get(user_id);            // get session var


You can test memory usage with memory_get_usage(). I would assume static classes use less memory though. But the difference is likely negligible. Not having access to your project, I could only encourage you to setup your own small test using the aforementioned function to see what the case is.


There will be a little overhead because an object needs to be created and placed in memory. But the question is, is it noticeable.

My opinion is that you should look to what works the most convenient. This kind of optimizations are mostly micro optimizations


In OO languages, static methods and objects have their own advantages and disadvantages. There may be minor differences in the amount of resources used by static methods and objects and their performance may differ from system to system based on the system configuration.

Objects can be declared and used multiple times unlike static methods which act like global variables. Even then the usage of objects and static methods depend on the project in hand.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜