Reputation: 523
As we all know, static methods can be called without instantiating the class. So I wonder if static methods will be loaded into memory before I use them. If that, in my view, I should use more intance methods instead of too many static methods. Anyone advice? I am not familiar with the underlying mechanism of PHP.
Upvotes: 5
Views: 2794
Reputation: 72177
A static method is just a regular function with a fancy name (and restricted access if it is not public
).
Static methods are not OOP, they are procedural code in disguise.
Should I avoid using too many static methods in PHP?
It depends how many do you think are "too many". For pure OOP code, one static method is already "too many". But sometimes it's unavoidable (read "easier") to write a static method for some functionality.
So I wonder if static methods will be loaded into memory before I use them.
No matter if you run a PHP script using the CLI or it is invoked through the web server to serve a web page, the text of the script is loaded into memory and compiled. If the compilation is successful (i.e. there are no syntax errors), the interpreter starts executing it.
Everything that is defined in the script is already in memory at this moment, but only the items defined in the main script. The inclusion statements (include
, include_once
, require
, require_once
) are not processed during the compilation phase.
The file referred by a include
statement is loaded in memory, compiled and executed when, and if, the include
statement is reached during the execution of the script. The entire content of the included file is loaded, parsed and converted to opcodes, no matter if it contains functions, classes or global code. There is no differences between instance methods and static methods from this point of view.
Upvotes: 8
Reputation: 8748
I should use more intance methods instead of too many static methods. Anyone advice?
Well sometimes you need to define a static method, if you need to call it without creating an object of that class but this should be limited.
When to use static methods:
You can use static methods as factories to create an object according to the context or sharing resources with other instances.
If there is no relation with your purpose and an instance
If you are planning to use public static properties, I would recommend to use CONST
in some cases unless you don't want to change its value.
Why?
Consts are on the class and object scope and its always immutable (You cannot change its value) and which is safe to use.
Static properties not on the object scope but on the class scope and its mutable (can be change) and which is not safe to use.
And alternatively you can check the Singleton pattern to see how to use static methods on resource sharing.
Upvotes: 1
Reputation:
So I wonder if static methods will be loaded into memory before I use them.
When a class is loaded, all of its methods are always loaded. This happens regardless of whether those methods are class methods or instance methods, or whether they are being used by the application.
Use whatever type of method is most appropriate for your design.
Upvotes: 5