Late Static Binding in PHP

Introduction

In this example I will explain late static binding in PHP. As of PHP 5.3.0, PHP implements a new feature referred to as late static binding that might be referred to as a category in the context of static inheritance. Late static binding introduces a new use for the static keyword.

More exactly, late static binding works by storing the category named within the last "non-forwarding call". Just as in the case of static methodology calls, this can be the category expressly named (usually the one on the left of the ":": operator); just as in the case of non-static methodology calls, it's the category of the item. A "forwarding call" may be a static one that's introduced by the "self::" keyword, "parent::" keyword, "static::" keyword, or, if increasing within the category hierarchy, "forward_static_call()". The operate "get_called_class()" are often used to retrieve a string with the name of the referred to the category and "static::" introduces its scope.

This feature was named "late static bindings" with an enclosed perspective in mind. "Late binding" comes from the actual fact that "static::" won't be resolved using the category wherever the strategy is outlined however it'll preferably computed using runtime data. It had been additionally referred to as a "static binding" because it is often used for (but isn't restricted to) static methodology calls.

Example

This example is used for late binding "Self::Usage" and static reference to the "self::" or "__class__", such as:

<?php

class mcn

{

   public static function emp()

       {

        echo __CLASS__;

    }

    public static function work()

       {

        self::emp();

    }

}

class we extends mcn

 {

    public static function emp()

       {

        echo __CLASS__;

    }

}

we::work();

?>

Output

limitation of self

Example

In this example I will explain late binding usage. Late static bindings tries to resolve that limitation by introducing a keyword that references the category that was at the start known at runtime. Basically, a keyword that might enable you to reference "hi" from yes() within the previous example. it absolutely was set to not introduce a replacement keyword, however; rather use static that was already reserved. In non-static contexts, the known as category will be the category of the article instance. Since "$this->" can try and decide personal strategies from an equivalent scope, use of "static::" might provide totally different results. Another distinction is that "static::" will solely talk over with static properties.

<?php

class mcn

 {

    public static function emp()

       {

        echo __CLASS__;

    }

    public static function work()

       {

        static::emp();

    }

}

class we extends mcn

{

    public static function emp()

       {

        echo __CLASS__;

    }

}

we::work();

?>

Output

late static binding

Example

This example uses a non-static context:

<?php

class mcn

 {

    private function emp()

       {

        echo "completed!"."<br>";

    }

    public function work()

       {

        $this->emp();

        static::emp();

    }

}

class we extends mcn

{

   /* emp() will be copied to we, hence its scope will still be mcn and*/

}

class you extends mcn

{

    private function emp()

       {

        /* original method is replaced; the scope of the new one is you */

    }

}

$data1 = new we();

$data1->work();

$data2 = new you();

$data2->work();

?>

Output

non static context

Example

This example shows forward and non-forward calls.

<?php

class mcn

 {

    public static function emp()

        {

        static::work();

     }

    public static function work()

        {

        echo __CLASS__."<br>";

     }

}

class we extends mcn

 {

    public static function good()

        {

        mcn::emp();

        parent::emp();

        self::emp();

    }

    public static function work()

       {

        echo __CLASS__."<br>";

    }

}

class you extends we

 {

    public static function work()

       {

        echo __CLASS__."<br>";

    }

}

you::good();

?>

Output

forward and non forward calls

For any problem related to static binding, please visit the http://www.php.net/ website.