News Feed
Sections




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

Matthew Weir O'Phinney's Blog:
Extending Singletons
February 06, 2007 @ 09:27:00

Matthew Weir O'Phinney wonders about singletons. He wonders how he can make them more flexible than they even are by making entire custom singletons accessible later, after they've been created.

This morning, I was wondering about how to extend a singleton class such that you could retrieve the new class when retrieving the singleton later. In particular, Zend_Controller_Front is a singleton, but what if I want to extend it later? A number of plugins in the Zend Framework, particularly view helpers and routing functionality, make use of the singleton; would I need to alter all of these later so I could make use of the new subclass?

He gives two code examples to illustrate his point - one that uses the current Zend_Controller_Front implementation to extend an instance and the other with a slightly modified version to do the same. The change is in the declaration of the $_instance value. By making it protected, the differently extended instance will be created. With the normal setting (private) you'll only ever get back the same default Zend_Controller_Front instance.

Check out the comments for some other suggestions as well.

0 comments voice your opinion now!
extend singleton controller front instance private protected extend singleton controller front instance private protected


blog comments powered by Disqus

Similar Posts

DevShed: Generating View from MySQL to Simulate the Model-View-Controller Schema in PHP

Greg Beaver's Blog: PHP namespaces become much better (Derick take note)

Alexander Netkachev's Blog: 404 error with Zend Framework Front Controller

Padraic Brady's Blog: An Example Zend Framework Blog - Part 5: Models w/Zend_Db & an Admin Module

DevShed: An Introduction to Simulating the Model-View-Controller Schema in PHP


Community Events





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


refactor podcast release threedevsandamaybe framework install wordpress language testing list configure introduction opinion developer interview code series laravel unittest community

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