PHP Late static binding in detail with examples

Spread the love

PHP Late static binding

PHP Late static binding is a very useful and important feature of object oriented PHP which is introduced in PHP version 5.3.

As the name suggests Late static binding is a binding related to static but it is Late 🙂

Confused?

Let’s break down the Late Static Binding.


What is Binding?

In programming, binding means when a method call is connected to the method body then it is called as binding.

In general whenever you call any class method then the body present inside that method will get executed.


What is Static binding?

Static binding is also called as Early binding. It is a simple type of binding. When a call to a method is made then the associated method body present in a class is decided and executed.

In this type, the method body association to the class is performed at the compile time.

Example:

<?php
 class Database
 {
   private $tableName = "Main";

  public function select()
  {
   echo "In select method of Database class accessing table = ".$this->tableName;
  }
  
 }
 $db = new Database();
 $db->select();
?>


The output will be:

In select method of Database class accessing table = Main

In the above example, the statement inside a method “select()” and the property $tableName is associated with the class “Database” at the compile time.


What is PHP Late static binding OR Dynamic binding?

Another type of binding is Late binding also called as Dynamic binding. It is opposite of static binding.

In Late binding, the method body association to the class is decided at the runtime, when an object is created.

This type of bind is present in an inheritance concept.

Let’s see the simple example:

<?php
 class Database
 {
  protected $tableName = "tblMain";
  
  public function select()
  {
   echo "In select method of Database class accessing table = ".$this->tableName;
  }
  
 }
 
 class User extends Database
 {
  protected $tableName = "tblUsers";
  
  public function select()
  {
   echo "In select method of User class accessing table = ".$this->tableName;
  }
 }
 $user = new User();
 $user->select();
?>


The output is:

In select method of User class accessing table = tblUsers

This is a simple inheritance example, we have seen this type of example earlier also.

In this example, though both parent class Database and child class User are having a same-named method “select()” and same property name â€ś$tableName”, but it is decided at runtime which method body will execute and which value of $tableName will be selected for an object of a User class at only runtime.

In this example, control decides the method to execute Late, as per the object created at the runtime.

Hence the property $tableName of User class, which is “tblUsers” is selected and a method body associated with the User class is executed at runtime.

But in the above program, we have used the “select” method in child class also which is a repetition of code.

So let’s remove the “select” method of child class as it is already present in the parent class and use the inheritance feature.

<?php
 class Database
 {
  protected $tableName = "tblMain";
  
  public function select()
  {
   echo "In select method of Database class accessing table = ".$this->tableName;
  }
  
 }
 
 class User extends Database
 {
  protected  $tableName = "tblUsers";
  
 }
 $user = new User();
 $user->select();
?>


Let’s see the output:

In select method of Database class accessing table = tblUsers

In this case, also the output is the same, but here control automatically called “select” method of parent class Database, BUT the property of a child class User which is “tblUsers”.

So in inheritance program with non-static members, is able to decide the members to use at runtime as per the object created.

Now, let’s change the property $tableName into static, in both parent and child class. And instead of $this-> we will need to use “self::” for static members.

<?php
 class Database
 {
  protected static $tableName = "tblMain";
  
  public function select()
  {
   echo "In select method of Database class accessing table = ".self::$tableName;
  }
  
 }
 
 class User extends Database
 {
  protected static $tableName = "tblUsers";
  
 }
 $user = new User();
 $user->select();
?>


The output is:

In select method of Database class accessing table = tblMain

Now here the output changed.

Even though we have created an object of a child User class, the property defined in child class “$tableName” is not used while calling “select” method using an object.

And parent Database class’s property is used and its value is printed i.e. “tblMain” instead of child class property value “tblUsers”.

So, the “self::” is not following the inheritance rules for static members.

So to overcome this problem of self:: and static members, the late static binding concept is introduced where the use of “static::” (static keyword with scope resolution operator) is used, instead of “self::”.

Let’s change “self::” to “static::” in the above program.

This change will implement the actual PHP Late static binding.

<?php
 class Database
 {
  protected static $tableName = "tblMain";
  
  public function select()
  {
   echo "In select method of Database class accessing table = ".static::$tableName;
  }
  
 }
 
 class User extends Database
 {
  protected static $tableName = "tblUsers";
  
 }
 $user = new User();
 $user->select();
?>


The output will be:

In select method of Database class accessing table = tblUsers

As you see, now we are getting the desired result.

Now the parent class is deciding at the runtime to use the static property of a class whose object is created.

Similarly, you can use the static methods in PHP Late static binding also to call appropriate methods at runtime.

In the next tutorial, you will learn about Traits in PHP.


Spread the love

Leave a Comment