Why we use static method in php

It is an interesting subject. I'm gonna give you a design oriented answer.

In my opinion, you should never use a static class/function in a good OOP architecture.

When you use static, this is to call a function without an instance of the class. The main reason is often to represent a service class which should not be instantiated many times.

I will give you 3 solutions [from the worst to the best] to achieve that:

Static

A static class [with only static functions] prevent you from using many OOP features like inheritance, interface implementation. If you really think of what is a static function, it is a function namespaced by the name of its class. You already have namespaces in PHP, so why add another layer?

Another big disadvantage is that you cannot define clear dependencies with your static class and the classes using it which is a bad thing for maintenability and scalability of your application.

Singleton

A singleton is a way to force a class to have only one instance:

The output for the above code snippet would be:

Kimono World

However, a static method can be accessed by a non-static method within the same class by the use of self:::

When you create a new instance of the class, the constructor will call the static function and output Hola!.

You can also access a static method in one class from another class by making the static method public.

This code will output:

Let’s see how this works

A static method can also be called from a child class [the concept of inheritance] by using the parent keyword within the child class. In this case, the static method could either be public or protected.

If you want to use a static method within subclasses, but not from outside the class, you can declare the method with the protected keyword.

In the code snippet above, the child class—address2—inherits from the parent class address. In the child class is a publicly declared variable that is accessed within the non-static constructor method __construct[]. The static method retrieveIPAddress is then accessed with parent::retrieveIPAddress. The IP address is returned as the output.

Static Classes

Basically, a class is an independent and self-contained data type that can contain data members called properties and member functions which can only be accessed by initializing an instance of that class.

Declaring a class as static allows the values it holds to remain the same for every instance of that class. A non-static class, on the other hand, can have different property values for each instance.

How to Create a Static Class

A class becomes static when the variables and methods defined within it are made static using the static keyword.

A static class and its defined static method[s] can be accessed by using the class name, the :: [scope resolution] operator, and the method name, as displayed in the snippet below.

name_of_class::name_of_method[];

In the above program, we have made both the $registrationNumberLength variable and the verifyRegNumber[] function static. Remember, you can only access static variables from static functions, and that way we will not have to instantiate the class at any point. It actually is so much easier to work with the variables and the functions statically in the sense that a general functionality is provided and the information used is not specific to the one student. Notice that when accessing the $course and $school variables, which are non-static, we returned them by using the this keyword and the -> arrow to imply that they are instances of the Student class.

So basically, the main difference between static members and the normal classes, methods, and variables when they are being accessed is the use of the double colon :: instead of the -> arrow operator.

Conclusion

In this article, you learned how to use static functions and static classes, and hopefully got interested in learning more about these concepts in the future. The static concept in PHP, and everything regarding it, should be clearer than ever now! Happy coding!

Did you find this post useful?

Technical instructor

Neema Muganga is an experienced technical writer and skilled web developer specializing in React.js and the Django stack. She has so far published several articles covering the different skill-sets she is proficient in—PHP and React.js—and has also written a number of Python how-to guide articles. Neema is also knowledgeable in API development with the Django Rest Framework, as well as systems development with PHP. She displays good collaboration in a team setting and can be relied upon for independent projects and time-sensitive tasks.

Chủ Đề