Home arrow PHP arrow Page 4 - Using Late Static Bindings in PHP 5.3

Fixing things with late static bindings - PHP

PHP 5.3 introduced a number of valuable features to the popular and constantly developing language, but some of them seem less useful at first glance than they actually are. Late static bindings fall into this category. This article series shows you how to unlock the true power of LSB to work for you.

  1. Using Late Static Bindings in PHP 5.3
  2. Why late static binding is so useful
  3. Attempting to grab an instance of an abstract registry class
  4. Fixing things with late static bindings
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
May 17, 2010

print this article



As I said before, LSB uses the keyword “static” to resolve at runtime conflicts that may occur in static contexts, like the one that you saw in the preceding section. To make the pertinent registry classes behave correctly, it’s necessary to tweak  their definitions. So first, here’s how the base “RegistryAbstract” looks when we use late static bindings within its “getInstance()” method:






abstract class RegistryAbstract


    protected static $_instances = array();


    // get Singleton instance of the registry

    public static function getInstance()


        // the static call to 'getClass()' is resolved at runtime

        $class = static::getClass();

        if (!isset(self::$_instances[$class]))


            self::$_instances[$class] = new $class;


        return self::$_instances[$class];



    // throw an exception as this class can't be instantiated

    protected static function getClass()


        throw new RegistryException('This class is abstract and cannot be instantiated!');



    // implemented by registry subclasses

    abstract public function set($key, $value);


    // implemented by registry subclasses

    abstract public function get($key);


    // implemented by registry subclasses

    abstract public function clear();         








(RegistryException.php) RegistryException.php)








class RegistryException extends Exception{}

As you may have noticed, the “getClass()” method is now called using the expression “static::getClass()” instead of the typical “self::getClass(),” which implies that the method will always be executed in the scope of the calling class. In other words, if the method is invoked by the abstract parent, it will raise an exception, which is the expected behavior; but when called inside the concrete array registry class, it will behave like this:









class ArrayRegistry extends RegistryAbstract


    private $_data = array();


    // save data to the registry

    public function set($key, $value)


        $this->_data[$key] = $value;

        return $this;



    // get data from the registry

    public function get($key)


        return isset($this->_data[$key]) ? $this->_data[$key] : null;




    // get called class

    public static function getClass()


        return __CLASS__;




    // clear the registry

    public function clear()


        $this->_data = array();



Here’s the amended version of “getClass().” Now, when called from inside the previous “ArrayRegistry” class, the “getInstance()” method will effectively return an instance of this concrete registry, thanks to the functionality provided by late static bindings. The implementation of this Singleton method could be simplified by using another handy function included with PHP 5.3, named “get_called_class(),” but the details of this process will be discussed in an upcoming tutorial of this series.

In the meantime, feel free to edit the source code of the previous registry classes. This process will arm you with a more solid background in working with LSB.

Final thoughts

In this first part of the series, I provided you with a concrete example that demonstrated how to use the feature called Late Static Bindings introduced in PHP 5.3.0. As you saw, it allows to solve problems that may arise when working with a static hierarchy of classes.

It’s fair to say, however, that the aforementioned example in its current state looks rather incomplete, since it doesn’t include a script that shows how to put the registry classes defined so far to work together. Since I plan to create such a script in the next tutorial, I recommend you don’t miss it!   

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: