Home arrow PHP arrow Page 3 - Registry Classes in Action with Late Static Bindings in PHP 5.3

The registry classes in action - PHP

In this second part of the series, I show you how useful late static bindings can be for dealing with a hierarchy of classes in static environments. In this case, the classes that compose that hierarchy are simple implementations of the registry design pattern, but this feature can be utilized in different situations and yield similar results.

  1. Registry Classes in Action with Late Static Bindings in PHP 5.3
  2. Review: definitions of previous registry classes
  3. The registry classes in action
  4. Including an autoloader class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
May 18, 2010

print this article



Now that you've grasped the underlying logic of using late static bindings with the prior registry classes, it’s time to build an example that demonstrates whether they're as functional as they look. With that idea in mind, below I coded a script that shows how to use an instance of the “ArrayRegistry” class to save and fetch some fictional users from RAM. Here it is:   

// include source classes

require_once 'RegistryAbstract.php';

require_once 'ArrayRegistry.php';



    // grab the Singleton instance of the ArrayRegistry

    $arrayRegistry = ArrayRegistry::getInstance();

    // save some users to the registry

    $arrayRegistry->set('user1', 'Julie Smith')->set('user2', 'Mary Wilson');

    // get the first user from the registry

    echo $arrayRegistry->get('user1');



    displays the following

    Julie Smith



catch (Exception $e)


    echo $e->getMessage();



That was a breeze to code, wasn’t it? As you could see above, using the array registry class is a extremely simple process that doesn’t bear any further discussion. I want you to pay attention to the line that grabs the Singleton instance of the registry via the “getInstance()” method, since it shows in a nutshell the real functionality of late static bindings.

Prior to PHP 5.3, this method should have been implemented directly within the concrete registry. But now, it lives comfortably inside the parent, thus making it possible to share it with multiple registry subclasses.

So far, so good. At this point the example illustrates that LSB is really a powerful feature that can be used, among other things, to create a hierarchy of registry classes. But as you may have noticed, the example starts manually including the source classes. This is pretty annoying, considering the nice autoloading capabilities that PHP hides under the hood. Therefore, in the last section I’m going to enhance the earlier example by adding to it a simple autoloader class.

To see how this autoloader will be defined, jump forward and read the following segment. It’s only one click away.         

>>> 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: