News Feed
Sections




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

Allan MacGregor:
Design Patterns in PHP Singletons
January 29, 2014 @ 09:25:55

Allan MacGregor has posted his latest in his look at design patterns in PHP with this most recent post about the Singleton pattern.

The singleton pattern is used to restrict the instantiation of a class to a single object, which can be useful when only one object is required across the system. Singletons are designed to ensure there is a single (hence the name singleton) class instance and that is global point of access for it, along with this single instance we have global access and lazy initialization.

He provides a basic Singleton implementation in PHP, a "User" class that always returns the same instance of itself no matter how many times the "singleton" method is called. He continues on and touches on one of the pain points around singleton use - many developers consider them an anti-pattern because their results can make it difficult to correctly test. He talks about how they break the Single Responsibility Principle (part of SOLID) and how they can hide dependency injection.

Singletons, Anti-patterns, and patterns in general are not good or bad; what makes a Singleton an Anti-pattern is not the pattern itself but how often is poorly implemented and how easy it is to do so.
0 comments voice your opinion now!
design pattern singleton introduction antipattern testing

Link: http://coderoncode.com/2014/01/27/design-patterns-php-singletons.html

blog comments powered by Disqus

Similar Posts

Symfony Project Blog: We're writing a book

Andi Gutmans\' Blog: New Zend Framework Website look launched (almost)

Chris Shiflett\'s Blog: Power PHP Testing

Ilia Alshanetsky's Blog: PHP 5.2.1 RC1 Available for testing

DevShed: Expanding an Error Logger with the Chain of Responsibility Pattern


Community Events





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


homestead introduction laravel application code series configure library language developer threedevsandamaybe api wordpress interview install community podcast unittest list release

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