A walk through into Late Static Binding in PHP

Hello everyone, do we remember the concept of Static Binding and Dynamic Binding, the one we studied in Java classes during our college time?

Yes!! Then very nice :). Today in this blog, we will go through those concepts using PHP for better understanding and visibility for the persons like me :D.

What is Binding?
In simple software language, “connecting a method call, to the method body is known as binding”.

 

Types of Binding
There are two types of binding

  1. Static binding (early binding)
  2. Dynamic binding (late binding)

 

Example of static binding in PHP
Here in this case, class association is made during compile time.

<?php
class Vehicle {
    public function color() {
        echo 'car color is RED';
    }
}

$vehicle = new Vehicle();
$vehicle->color();
?>

This would print the output as : car color is RED

 

Example of dynamic binding in PHP
Here in this case, class association is not made until the object is created at execution time.

<?php
class Vehicle {
    public function color() {
        echo 'car color is RED';
    }
}

class Honda extends Vehicle {
    public function color() {
        echo 'car color is BLUE';
    }
}

$vehicle = new Honda();
$vehicle->color();
?>

This would print the output as : car color is BLUE

 

But, have you heard of another binding called “Late Static Binding”!! No…. then let’s go through that 🙂 with some interesting example.

What is Late Static Binding?
PHP 5.3 implemented a new feature called Late Static Binding, which is used to reference the called class regarding to static method inheritance.

 

Why the name Late Static Binding?
It is the combination of two concepts i.e. Late Binding and Static Binding.
Late binding comes from the fact that static:: keyword will not be resolved using the class where the method is defined but it will rather be computed using runtime information.
Static binding comes from the fact that it can be used for (but is not limited to) static method calls.

 

Why it came into the picture?
To overcome the use of self keyword, Late static binding concept came. self does not follow the rules of inheritance and it always resolves to the class in which it is used. For example – if you make a method in a parent class and call it from a child class, self will always reference the parent, instead of child.

But in case of Late Static Binding, static keyword has been used extensively to represent the class where it is first used i.e. it binds to the runtime class.

 

Example of late static binding in PHP with self, parent and static

<?php

class Mango {       
    function classname(){
        return __CLASS__;
    }
    
    function selfname(){
        return self::classname();
    }
    
    function staticname(){
        return static::classname();
    }
}

class Orange extends Mango {     
    function parentname(){
        return parent::classname();
    }
    
    function classname(){
        return __CLASS__;
    }
}

class Apple extends Orange {     
    function parentname(){
        return parent::classname();
    }
    
    function classname(){
        return __CLASS__;
    }
}

$apple = new Apple();
echo $apple->selfname() . '<br/>';
echo $apple->parentname() . '<br/>';
echo $apple->staticname();

?>

This would print the output as :
Mango
Orange
Apple

 

Example of late static binding in PHP with forward_static_call()

<?php

class Mango
{
    const NAME = 'Mango is';
    public static function fruit() {
        $args = func_get_args();
        echo static::NAME, " " . join(' ', $args) . "<br/>";
    }
}

class Orange extends Mango
{
    const NAME = 'Orange is';

    public static function fruit() {
        echo self::NAME, "<br/>";
        
        forward_static_call(array('Mango', 'fruit'), 'my', 'favorite', 'fruit');
        forward_static_call('fruit', 'my', 'father\'s', 'favorite', 'fruit');
    }
}

Orange::fruit('NO');

function fruit() {
    $args = func_get_args();
    echo "Apple is " . join(' ', $args);
}

?>

This would print the output as :
Orange is
Orange is my favorite fruit
Apple is my father’s favorite fruit

 

Example of late static binding in PHP with get_called_class()

<?php

class Mango {
    static public function fruit() {
        echo get_called_class() . "<br/>";
    }
}

class Orange extends Mango {
    //
}

Mango::fruit();
Orange::fruit();

?>

This would print the output as :
Mango
Orange

So, did you get some idea of method overriding or binding? Do you really find it useful? 🙂 If yes, then please like and add some comments, if you want.

Thanks 🙂 and will be happy to listen from you 🙂 :).

Advertisements

One thought on “A walk through into Late Static Binding in PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s