The Developer Day | Staying Curious



PHP Anti Patterns

Another talk I’ve atended at PHPUK 2010 was AntiPHPatterns by Stefan Priebsch. While design patterns are core implementation independent solutions to problems that occur over and over again which also serve as a great vocabulary, anti patterns are software patterns that are ineffective or counterproductive. In his presentation Stefan describes some of these anti patterns:

1. Constantitis. Excessive use of global constants is considered to be a code smell. Global constants can be defined anywhere in the code base, there is a risk of name clashes if a constant is already defined, global constants make the code more coupled, testing gets more complicated since constants have to be known beforehand and defined explicitly which might be even more troubling if a constant has to change it’s value for another test. Since class constants are not global it’s OK to use them. Cure for constantitis is not to use global constants and instead use dependency injection.

2. Globalomania. Global variables share the same problems as global constants. Because global variables can be changed it makes them more dangerous than global constants since a change in one part of the codebase can affect the other without anyone noticing. Global variables can be cured by using dependency injection.

3. Singletonitis. Singleton is one of the most popular design patterns. It’s wide success is due to the fact that singletons by implementation are available globally in the entire application. The problem that singleton design pattern tries to solve is to prevent having multiple instances of the same class. This is rarely the problem in most applications and most singletons are being used as global variables instead. Singletons share the same problems as global constants and global variables and therefore should be avoided. Singletonitis has the same cure as constantitis and globalomania.

4. God classes. According to object oriented best practices classes should do one thing only and do it well. Classes should be refined and granular. One of the ways to think about this is to ask yourself what are the responsibilities of this class. In an ideal case you will be able to describe it in one sentence without any “and’s”. When classes start having too many  responsibilities they become god classes. Usually the whole application relies on one of the god classes which makes the application tight coupled and therefore more difficult to maintain. To cure god classes minimize class responsibilities so that objects know everything about themselves and little about others.

RSS Feed

4 Comments for PHP Anti Patterns

Paul G. | March 10, 2010 at 9:07 AM

I am reading your article with great interest.

I can very much agree with what you say about constantitis, globalomania and god classes, but when it comes to singletons, I can’t find a better solution than using a singleton. Without it I will have to pass important instances as arguments to all classes that needs it.

Ofcourse, some of the classes that are, and should be loosely coupled, must get a database instance by argument, but for the rest (controller classes and other classes in an application) can just as well get the database instance from the singleton, and pass it as argument to the loosely coupled classes.

Do you have a suggestion for what to do instead of using a singleton?


Author comment by Žilvinas Šaltys | March 10, 2010 at 9:48 AM


You must agree that singletons are in some way the same global variables which you agree are bad practice.

Then you have to think about tests. While you can still do unit tests with singletons it’s not as straightforward as with dependency injection.

You could also check dependency injection frameworks such as which might make it a lot more easier to instantiate your application objects without always passing dependencies.

How you instantiate objects in controllers doesn’t matter much. For example most of the older Zend Framework code will use Registries. The new generation may use DI containers or resource loaders.

Misko Hevery has written a lot about singletons, testing and global state. You might be interested to check his blog:

Somtimes we could learn from the bad practises | Joshy's microblog | October 10, 2011 at 9:03 AM

[...] we could learn from the bad practises Posted on 2011. October 10. Monday by admin PHP Antipatterns This entry was posted in Link and tagged php. Bookmark the permalink. Post navigation← [...]

2012 Recap | It's Majax | December 31, 2012 at 6:47 PM

[...] up on the important things core to being a high quality software developer: Patterns, Anti-Pattens, and Code Smells, among other things. These will teach you the do’s and don’ts of our [...]

Leave a comment!



Find it!

Theme Design by