PHP: Static Properties & Methods

When a new object is created out of a class, it has a copy of the class's properties.

But in PHP, a class can also contain properties which belong exclusively just to the class and not to any instance of it. These properties are known as static properties, and to differentiate them from instance/object properties they are also called class properties.

static properties methods Business vector created by Freepik

Static Properties and self::

We declare a class called Relay consisting of a static variable $time, which is assigned the value 0. Static properties are defined inside a class using the static keyword.

					
						<?php 
							class Relay {
								static $time = 0;					
							}										
						?>
					
				

Accessing a PHP static property outside the class do not necessiate the creation of an object; you just have to qualify it with the class name followed by the double colon :: operator, known formally as the scope resolution operator.

					
						<?php 
							echo Relay::$time;				
						?>
					
				
usain bolt Usain Bolt at the 2012 Summer Olympics, London. Photo by Nick Webb from London. CC BY 2.0

Now we go on to illustrate one use of a static property with an example. Consider a 4 x 100 metres relay race run by a team of four sprinters, each sprinter covering a leg of 100 metres. The world record on the race so far was created by the Jamaican quartet consisting of Nesta Carter, Michael Frater, Yohan Blake and Usain Bolt, who all won Gold at the 2012 Summer Olympics in London.

The time taken (in seconds) by each of them to complete their respective legs, as per the video here, are listed below:.

  • Nesta Carter — 10.10
  • Michael Frater — 8.90
  • Yohan Blake — 9.00
  • Usain Bolt — 8.80

The static property $time was actually created to store these incremental time values. We add the __construct() function to the Relay class above.

Accessing a static property inside one of the class's methods require the prefix self:: to be used. Now with each creation of an instance, the $time static property inside __construct() is incremented by the time taken by each sprinter to complete their respective leg.

					
						<?php 
							class Relay {
								static $time = 0;														
								function __construct($t) {
									self::$time += $t;								
								}
							}

							$nesta = new Relay(10.10);				
							$michael = new Relay(8.90);						
							$yohan = new Relay(9.00);					
							$usain = new Relay(8.80);							
						?>
					
				

If we print the value of $time after creating the above four instances, we get the value 36.8.

					
						<?php 
							echo Relay::$time;								
						?>
					
				

Had the $time property been declared public, accessing its value outside would still have given 0.

Static Methods

Similar to static properties, static methods also belong to the class and are not bound to any of its instances. And since they are not bound to any instance of a class, the $this variable is not available in them. So if you need to invoke a static method inside another method of the same class, use self::. And just like static properties, we do not require creating any object to invoke them; instead, we use the scope resolution operator :: after a class name to call them.

					
						<?php 
							class Relay {
								static $time = 0;														
								static function addSprintTime($t) {
									self::$time += $t;								
								}
							}

							Relay::addSprintTime(10.10);
							Relay::addSprintTime(8.90);
							Relay::addSprintTime(9.00);	
							Relay::addSprintTime(8.80);

							echo Relay::$time;									
						?>
					
				

Class Constants

As you might have come across in php-constants.html, the define() function allows us to define constants globally. But since PHP 5, constants can be defined inside classes using the const keyword. And akin to static properties and methods, class constants also belong just to the class and not to its instances. And so to access them inside a method, we use self::

Class constants are case-sensitive.

					
						<?php 
							class Irrational {
								const PI = 3.141;							
							
								function getPi() {
									return self::PI;
								}
							}

							$pi = new Irrational();
							echo $pi->getPi();				
						?>
					
				

The Reserved Words self:: and parent::

We already have come across self:: many times above to access static members and class constants. There is another keyword called parent:: which is used to access properties, methods, constructor and constants of the parent class.

					
						<?php 
							class Irrational {
								const PI = 3.141;
							}

							class PI extends Irrational {
								function fetchPi() {
									return parent::PI;
								}
							}

							$pi = new Pi();
							echo $pi->fetchPi();			
						?>
					
				

Notes

  • Class constants are case-sensitive. But it is recommended to write class constants in upper-case letters.