Home arrow PHP arrow Page 4 - The Get_called_class() Function in PHP 5.3

Putting the registry classes into action - PHP

In this third part of a series on late static bindings, you will learn how to use the “get_called_class()” function bundled with PHP 5.3 for determining at runtime which class has been called in a static context. In certain situations this function can be used as a replacement for late static bindings, but LSB has a wider range of possible uses; keep this in mind when developing your own object-oriented applications.

TABLE OF CONTENTS:
  1. The Get_called_class() Function in PHP 5.3
  2. Review: using Late Static Binding to implement the registry pattern
  3. Using the get called class function
  4. Putting the registry classes into action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
May 19, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In keeping with the concepts deployed before, below I coded a script very similar to the one shown in earlier parts of the series, which proves that the functionality of the previous registry classes remains the same, even though some methods have been modified or removed. Look at the following code snippet, please:

<?php

try

{

    require_once 'Autoloader.php';

    // grab the Singleton instance of the autoloader

    $autoloader = Autoloader::getInstance();

    // grab the Singleton instance of the ArrayRegistry class

    $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();

    exit();

}

Well, if you take the time to test the above script on your own web server, it should work like a charm. What’s more, I encourage you to derive a few additional concrete registries from the abstract parent and use the “getInstance()” method with each of these classes. Provided that you have already installed PHP 5.3.0 (or above) on your machine, in all  cases the method should return an instance of the class that has been called from client code.

In summary, late static bindings are a useful feature that allows you to build more efficient hierarchies of classes, and in the appropriate context they can be implemented by using the equivalent “get_called_class()” function. However, don’t overuse them; stick when possible to one of the great commandments of object–oriented programming: “Favor Composition over Inheritance.”     

Final thoughts

In this third chapter of the series, you learned how to use the “get_called_class()” function bundled with PHP 5.3, which as it name suggests, comes in handy for determining at runtime which class has been called in a static context. In certain situations this function can be used as a replacement for late static bindings, but LSB has a wider range of possible uses, so keep this in mind when developing your own object-oriented applications.

In the next part of the series I’m going to recreate yet another use case where LSB can be applied successfully: the implementation of the abstract factory design pattern. So here’s my final suggestion: don’t miss the upcoming tutorial!  



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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