views:

66

answers:

3

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

VS


Static methods from Session class

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

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.

Jonathan Sampson
+2  A: 

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

Ikke
A: 

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.

thinktejas