PHP Patterns, Part I

Missing Image
By SiteCrafting Staff

This is the first in a planned series of articles discussing the application of various PHP patterns. If you're still getting your feet wet developing in PHP (or programming languages in general), you may be wondering what a "pattern" is in the first place. The best place to find a quick explanation is over in Wikipedia's article (which does a much better job than I could hope to do within the scope of this article).

Now, let's move on to the fun part . . .

I. The Registry Pattern, an Introduction
I am going to kick off this series by introducing you to the registry pattern. First, imagine you are deep into a complex web application and you come to the realization that you have several objects that all require access to your database. Your first inclination may be to simply have each class initiate their own connection to the database and use it as they please. But then you'll start to hear that little voice in the back of your head, screaming something about how you're needlessly wasting valuable system resources and bandwidth. Naturally you ask what else you could do - this is where  a registry comes in handy.

A registry object basically acts as a container for objects that you want to be universally accessible throughout your project, while avoiding the use of . . . gulp . . . global variables! You'll find that its implementation is fairly simple, but extremely powerful and convenient.

II. Class Definition
Let's take a look at what a registry class is made of:

class Registry {
$_objects = array();

set($name, &$object) {
$this->_objects[$name] =& $object;

    function &
get($name) {

Look at that! Pretty short and sweet eh? Now let's rip it apart and see what each component does:

  1. Following the class definition, we have a protected class variable called $_objects which is an array that holds all the objects that are added to the registry.
  2. Next we find our first function called set which is inherently public. This function is used to add an object instance to the registry. You pass it the object instance, and pick a friendly name to remember it by (don't forget it, you'll need it later!).
  3. Finally we have our second and final function get which retrieves an object previously added to the registry. Simply supply the friendly name you picked earlier and it will return the instance to you.

    You may be wondering what that ampersand (&) is doing up in front of the function name. This tells the function to return the object by reference rather than making a copy of it and returning the copy (which would make all this work pointless, since we again would be wasting resources). This means that any changes or actions performed on the object will be reflected everywhere else it is used in your application.

III. Less Talk, More Action!
Now that we have the basic concept outlined, let's take a look at how we might use our new creation. Let's say we have a simple class that returns a user's name, and another class that checks if a given user has "admin" status. In practice you would probably group this functionality into a single class, but for the sake of illustration we will pretend Captain Obvious is on vacation:

// First we create our database and registry
// objects
$db = new databaseConnection();
$registry = new registry();

// Then we add the db object to the registry
// It will now be available anywhere the registry
// is also available!
$registry->add("database", $db);

// Now we create the objet that gets our username
// and checks for admin status
$user = new username($registry);
$admin = new admin($registry);

$username = $user->getUser(12);
$isAdmin = $admin->check($username);

// Finally, we display the user's info
echo("Username: ".$username."<br />");
"Is Admin? ".$isAdmin);

Now, you'll have to use your imagination to see how the two objects are able to use the same database instance, but let's take a peek into what they both do:

//----- snip -----

// First they get the database object from the
// registry
$databaseObj = $registry->get("database");

// Then they use it as they see fit
$databaseObj->query("SELECT username FROM users".
    " WHERE id=$userId"

//----- snip -----

Congratulations, you have just learned how to create and use a registry! Now go forth and use what you have learned to write more efficient code!


Back To Feed