News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Gonzalo Ayuso's Blog:
Moving singleton and factory patterns to Abstract with php 5.3
January 27, 2010 @ 13:55:12

Gonzalo Ayuso came across a situation where he needed to create a factory method inside of multiple classes but didn't want to have to create them each time. With class inheritance there had to be another way.

I have built a backend library. I have a tree of classes and i want to use singleton and factory patterns to my class set. Easy isn't it? [...] Now imagine you have a lot of classes. You must create over and over the factory function in every classes.

An abstract class is a perfect fit for this sort of thing, but there's not a good way to return the correct kind of object back from the factory method. That is, until you do some magic with a PHP installation version 5.3 and higher. With the get_called_class function, you can easily figure out where the request came from and return a correct object instance with one line.

0 comments voice your opinion now!
abstract singleton factory pattern tutorial


blog comments powered by Disqus

Similar Posts

Padraic Brady's Blog: Writing A Simple Twitter Client Using the PHP Zend Framework's OAuth Library

Andreas Gohr's Blog: A Pirate Map with LibGD and Google Maps

SitePoint PHP Blog: Getting Started with Medoo Examples of Use

PHPBuilder.com: Building an Error-Handling Mechanism

HowTo Forge: How To Harden PHP5 With Suhosin On Fedora 7


Community Events

Don't see your event here?
Let us know!


introduction symfony community laravel5 release extension library series voicesoftheelephpant unittest opinion version podcast threedevsandamaybe interview laravel framework api language security

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework