Skip to main content

Late Static Binding in PHP

With release of PHP 5.3 functionality of late static binding was introduced. Late static binding in PHP can be used to reference the called class in a context static inheritance. Before diving deep into this topic lets discuss the keywords like self and static in PHP .

In PHP, you use the self keyword to access static properties and methods of classes.Self is resolved at compile time, which means that when class is inherited from another class, self will always relate to the class where it is mentioned not the class which is inheriting it. Here is example of use of self

class A{
public static  function display(){
echo 'function called using self';
}
public function foo(){
self ::display();
}Minify General Setting
}
$ob =  new A();
$ob->foo();
?>
output is
function called using self

Limitation of  self


<?php
class A{
public static  function display(){
echo self::get_classname().'</br>';
}
public static function get_classname(){
return __class__;
}
}
class B extends A{
public static function get_classname(){
return __class__;
}
}
$oA =  new A();
$oA->display();
$oB =  new B();
$oB->display();
?>
output is
A
A

As shown in above example, self is referring to the class A(Base Class) in both cases and function  get_classname from class A has been called Twice.
To overcome the limitation, late static binding comes to the picture. Late static binding tries to solve this limitation by introducing a keyword that references the class that was initially called at runtime. It was decided not to introduce a new keyword but rather use static that was already reserved. In the above example by just replacing the self keyword with the static output of the code will change as following.


<?php
class A{
public static  function display(){
echo static::get_classname().'</br>';
}
public static function get_classname(){
return __class__;
}
}
class B extends A{
public static function get_classname(){
return __class__;
}
}
$oA =  new A();
$oA->display();
$oB =  new B();
$oB->display();
?>
output is
A
B

 Use  of late static binding

In the following example, singleton class is created using the late static binding.With the help of late static binding here in the following program we have choice of using same get_instance function to create the objects of both classes(Singleton class and B class ). get_instance function checks to see if an instance of the class has been created yet or not. If it hasn’t it creates a new instance. If it has been created before, it returns the singleton instance which was previously created. The other important part is that you need to make the constructor function “private”. This ensures that the class can not be instantiated outside the class, only the get_instance function can be used to create it.

<?php
class singleton {
public static $instance;
private function __construct(){
echo 'Contruct of  singleton class called</br>';
}
public static function get_instance(){
if( !static::$instance instanceof static)
{
static::$instance = new static();
}
return static::$instance;
}
public function test (){
echo 'test function called</br>';
}
}
class B extends singleton {
public static $instance;
protected function __construct(){
echo 'Construct of Class B called</br>';
}
public static function get_class_name  (){
return __CLASS__;
}
}
singleton::get_instance()->test();
B::get_instance()->test();
B::get_instance()->test();
?>
output is
Contruct of singleton class called
test function called
Construct of Class B called
test function called
test function called

To read further about late static binding in php you can refer to below URL:
http://www.php.net/manual/en/language.oop5.late-static-bindings.php

Support Me by Sharing This Article

Leave a comment/Ask Question

3 thoughts on “Late Static Binding in PHP

shares