tags:

views:

46

answers:

2

If I have a class called myclass does having static methods within the class affect its size in memory?

class myclass{
public $instancevar;
public static function method1(){}
public static function method2(){}
}

Will the addition of more static methods make instances of myclass larger?

I know that static methods are shared between instances of a class but will adding more or larger static methods impact on the size of the object in memory at all? Similarly will having more static methods affect execution time if I pass an instance to a method as an argument?

A: 

Static methods shouldn't increase the memory footprint of your object.

The question is - would you even notice if the static methods did increase the memory footprint of your object?

Frank Shearar
I wouldn't but someone else has taken issue with my use of "enormous" objects that are actually only half a dozen member variables and a few large-ish static functions. I just wanted to check my understanding :)
andyjdavis
+1  A: 

I would even say that also member methods to not increase the memory footprint of an object. If I serialize an object, only the information from which class it is an instance and which values its properties have are serialized. This is enough to determine the objects behaviour.

You can compute the size of an object described in this question.

You will see that adding or removing static or non-static methods does not change the object's size.


To sum up: You gain nothing if you use static methods that operate on the object than using object methods directly.

Felix Kling