5

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.

4
  • 1
    Yes, you should avoid too many static methods in PHP, but not for memory management reasons. Because it's a signal you're not doing OOP right. And this will hurt you more in the long run than a few extra bytes of used ram (which I'm not sure is even the case). Nov 5, 2017 at 6:43
  • 1
    The question of using a static method or working with objects is typical one of the actual scope of what logic you try to implement. That should be your first concern: that people can understand the logic when it gets more complex, that you are able to implement special cases without making the code appear bloated by too many runtime arguments. Questions like memory footprint should not always be ignored, but they usually are not that important any more these days. If you work in an environment having issues here, then why do you use OPP at all?
    – arkascha
    Nov 5, 2017 at 6:51
  • of course, from the perspective of OOP, I should avoid too many static methods. just wonder if they will be loaded into memory before I use them?? Nov 5, 2017 at 6:53
  • 2
    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. The code of the main script is loaded in memory and compiled when the page is requested. It loads in memory and compiles the include-d files when (and if) the include statement is executed.
    – axiac
    Nov 5, 2017 at 7:09

3 Answers 3

8

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.

1
5

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.