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
$object = new MyClass(); $object.someMethod();
We can write
MyClass::someStaticMethod();
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.
<?php class SomeClass { //Declaring a static variable public static $design_name = 'Kimono World'; //Declaring a static function public static function newFunction() { echo self::$design_name; } } SomeClass::newFunction() ?>
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::
:
<?php class regards { public static function greetings(){ echo 'Hola!'; } public function __construct(){ //accessing the greetings static function through the construct() non-static method self::greetings(); } } new regards(); ?>
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.
<?php class FirstClass { public static function firstFunction(){ echo 'Let’s see how this works'; } } class SecondClass{ public function functionTwo(){ FirstClass::firstFunction(); } } $class1 = new SecondClass; echo $class1 -> functionTwo(); ?>
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 of the class, you can declare the method with the protected
keyword.
<?php class address { protected static function retrieveIPAddress() { return '197.254.48.214'; } } class address2 extends address { public $IPAddress; public function __construct(){ $this->IPAddress = parent::retrieveIPAddress(); } } $address2 = new address2; echo $address2 -> IPAddress; ?>
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();
<?php class Student { public $school; public $course; public static $registrationNumberLength = 13; public function Details() { return $this->course . 'course belongs to the school of' . $this->school; } public static function verifyRegNumber($registrationNumber) { if (strlen($registrationNumber) >= self::$registrationNumberLength) return true; else return false; } } $registrationNumber = 'J17/0798/201'; if (Student::VerifyRegNumber($registrationNumber)) echo 'Registration Number length is correct'; else echo 'Registration Number length is invalid'; ?>
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!