Understanding Static Functions and Static Classes in PHP

* { box-sizing: border-box; } body {margin: 0;}*{box-sizing:border-box;}body{margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;}

Static functions and static classes can be easily accessed without necessarily creating an instance of that class. This helps keep your code clean and organized.

In this post, you’ll learn about the static keyword and how it is applied on functions and classes to make them static. Then, I’ll explain why you might want to implement static functions and classes in PHP.

By the end, you’ll understand how to make functions and variables within a class static, how to access these static members, and when to declare class members as static.

Using the static Keyword with A Method or A Variable

static is a special keyword PHP. Where it is used on methods or class properties, it allows them to be accessed on the class itself instead of on an instance of that particular class. So instead of writing

We can write

Therefore, whenever a method is declared as static, it can easily be accessed without the need to create an object for the class.

Of course, this only works for methods that don’t need to use any data from the instances of a class. Declaring a method as static limits it to only access static methods and static variables.

Declaring a Static Function and A Static Variable

The code below declares a function named newFunction as static by adding the static keyword before the function’s name. A property named design_name is also made static by including the static keyword before its name. When referencing the property within the static function, the scope resolution operator :: and the self keyword are used.

It is impossible for a static method to access a non-static variable and or  non-static methods because these require an instance of the class to be created.

The output for the above code snippet would be:

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:

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 of 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.

In the above program we have made the $registrationNumberLength variable static and the verifyRegNumber() function to be static as well. 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 static members have from 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 used 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, is clearer than ever now! Happy coding!

Leave a comment

Your email address will not be published.