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:
The reason why singleton is a "problem" with PHPUnit
September 24, 2012 @ 11:57:02

Gonzalo Ayuso has a new post that responds to the idea that "singletons are a problem when testing" your applications with something like PHPUnit.

Maybe this pattern is not as useful as it is in J2EE world. With PHP everything dies within each request, so we cannot persist our instances between requests (without any persistent mechanism such as databases, memcached or external servers). But at least in PHP we can share the same instance, with this pattern, in our script.

He illustrates a bad side effect of this sharing of resources with a simple unit test that increments a counter in a class. He notes that, because the script shares the object, you can't reliably know the state of it as you don't know what's happened before your use. He recommends two things to help the situation - either not use them at all or destroy the instance each time after using it (counterproductive to using a Singleton, obviously).

0 comments voice your opinion now!
singleton designpattern problem resource sharing


blog comments powered by Disqus

Similar Posts

DevShed: Introducing Mediator Classes in PHP 5

Stefan Koopmanschap's Blog: Adding a link to a form element with Zend Framework

Hasin Hayder's Blog: FeedPHP is going to be the largest PHP News Source

Pádraic Brady's Blog: Complex Views with the Zend Framework - Pt 5: The Two-Step View Pattern

Rafael Dohms' Blog: Problem Solving technique #1: Taking a mental break


Community Events

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


api library framework conference development release introduction psr7 opinion laravel video laravel5 community series extension interview voicesoftheelephpant podcast language unittest

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