Click here to Skip to main content
15,886,664 members
Articles / Programming Languages / PHP

A Walk Through Into Late Static Binding in PHP

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
15 Dec 2014CPOL2 min read 16.8K   6   3
A walk through into late static binding in PHP

Do you 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 people 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
<?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
<?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 the 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
<?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
<?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
<?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 I will be happy to hear from you :).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Software engineer with around 6 years of web application development experience in open source technologies like PHP, MySQL, HTML, HTML5, CSS, CSS3, Javascript, jQuery etc.

I love to learn and share my knowledge in which manner I can and like to solve the issues as in the coding perspective. I am an active contributor in community forums like StackOverflow, CodeProject etc. Besides that, I write blogs in free time and speak in various technical community events.

Comments and Discussions

 
QuestionNice info Pin
Testing Dataweb1-Jan-15 2:54
Testing Dataweb1-Jan-15 2:54 
AnswerRe: Nice info Pin
Prava-MFS1-Jan-15 3:02
professionalPrava-MFS1-Jan-15 3:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.