Home arrow PHP arrow The Get_called_class() Function in PHP 5.3

The Get_called_class() Function in PHP 5.3

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

If you ask a group of developers what’s so exciting about PHP 5.3, it’s very probable they'll tell you that this major upgrade of the language now supports native namespaces and anonymous functions. While these enhancements have been widely commented on and discussed in blogs and forums everywhere, PHP 5.3 also includes a rather overlooked feature called Late Static Bindings (LSB). This powerful tool allows you to easily solve common issues that occur when dealing with hierarchies of classes in a static context.

As with many other facets of PHP, by far the best way to understand how LSB work and why they’re so useful under certain conditions is by example. In the two previous articles of this series I explained how to use this feature to create a simple hierarchy of registry classes. In that particular case, the hierarchy was composed of an abstract parent, which implemented a static Singleton method called “getInstance(),” and a concrete array-based registry that obviously inherited the method in question.

This is Inheritance 101, so what’s the big deal? Well, prior to PHP 5.3, the aforementioned “getInstance()” method always would attempt to return an instance of the parent registry, thus throwing a fatal error because this class was declared abstract. However, thanks to the functionality of LSB, it is possible to implement the method so that it now returns the instance of the class from which it’s has been called. That’s very useful, indeed.

It’s valid to point out that in many cases, LSB is used with the constant __CLASS__ to resolve at run time which class has been referenced by a given method. Nevertheless, PHP 5.3 comes bundled with a whole new function named “get_called_class()” which performs a similar task, therefore simplifying the implementation of certain methods, like the one discussed above.

If you’re interested in learning about this function, in this third installment of the series I’m going to explain how to use it with the registry classes defined in the previous tutorials.

Keep reading to start learning how to put the “get_called_class()” function to work for you! 



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