In one of my questions about PHP programming, several people had commented that the programming in large PHP-based projects is not optimal. Amongst the projects not to look at are Drupal, Joomla, and Wordpress (commenters and answerers say). What is an example of a PHP project in which the programming is first rate, then?

More importantly, why? What is GOOD PHP programming about?

If you ask me about good Java code or C# code, I can answer that easily, but in an interpreted page-based language, I'm not so sure about good programming...


Note, this is a highly subjective area.

First, go read this article (yes, I know the question is about PHP; we'll get there)

Python is not Java

In particular, pay attention to this passage on getters and setters.

In Java, you have to use getters and setters because using public fields gives you no opportunity to go back and change your mind later to using getters and setters. So in Java, you might as well get the chore out of the way up front. In Python, this is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class. So, don't write getters and setters.

The basic gist here is, in a void, getter and setters aren't "good" or "bad". In the context of python they're bad because they're unnecessary. Python's property build-in (kind of like __get in php) lets you define a getter and setter at any point for an existing field. In the context of java they're good, because java has nothing like the property built-in or __get. The design of the language encourages a particular coding style.

PHP isn't a designed language. It started as a way to provide scripting access to low level C functions and some form processing. It has always been a glue language that takes a kitchen sink approach to language features. PHP3 and PHP4's idea of OOP was objects as collections of properties and methods. PHP5 went a radically different direction and brought in Java style Objects as references. PHP 5.3 is going to further add to the confusion by bringing in closures which will encourage more functional style coding.

So what's elegant code in an inelegant language? You need to develop a philosophy on a team by team basis. Do you want your PHP to resemble Java style coding, take on more functional conventions, or be straight up procedural? The team needs to make a choice than then stick to it. This is one of the reason Frameworks (Code Igniter, Symphony, Cake) have become so popular in PHP. They provide an architecture philosophy and a community of developers to come up with coding conventions with that architecture philosophy.

In a corporate environment, this is hard to do. An individual employee, without guidance, is going to code like they leaned to code in school and in their own career. PHP will let them get away with this and achieve positive, short term results at the cost of long term maintainability/elegance.

Finally, as an aside, I'd argue that Drupal and Joomla are, in parts, very well engineered. They took a mostly procedural, defuat global namespace PHP4 and via much clever work brought capital DP Design Patterns to the table.

9 accepted

I recommend Zend Framework. The criteria that I use to evaluate code quality is

  • DocBlock. It is very essential to know what a function, method, constant or class does. DocBlock addresses communication between programmers. Without it, the code is dead one.

  • Consistent naming convention: PHP Coding Standard does exist. Every PHP developers are recommended to use camelCase style for variables, class attributes, method/function parameters and CamelCase for class naming. You are also recommended to use ALL_CAPS for constant naming, alllower for namespace naming.

  • Code formatting: Un-formatted code is really a mess.

  • Descriptive names: Functions, classes, attributes/properties, variables should be named after what they actually do.

  • Single responsibility: A function or a method tries to do one thing which is described in its DocBlock.

  • Separation of concerns: MVC is only part of separation of concerns. Good developers can decide not to go with MVC but they should know how to separate concerns in their applications. If you see a code fragment where lot of concerns is mixed and can not be separated (business workflows, application flows, data access, validation, view rendering logic, html structure ...), you can be sure that it is a signal of not so good code (maybe bad one).

  • Global variables: This is a signal of bad code which can be seen in Drupal. The developers behind Drupal find difficult to choose a way to manage variable life cycle. Global variable makes Drupal code a mess.

  • Consistent error handling

Drupal, Joomla, Wordpress is not so good in code quality. Don't get me wrong. Drupal, Wordpress, Joomla are good at features, usability, GUI and everything that can make them very good products to endusers but in term of engineering, they have a lot of weaknesses.

pcdinh AT phpvietnam


While PHP is interpreted rather than compiled, its code quality can generally be judged by the same best practices found in many other procedural languages.

  • Small loosely coupled functions that have a single purpose
  • Consistent and descriptive variable, function, class names
  • Proper use of encapsulation and information hiding, etc (assuming OO). List goes on.

If you're truly asking for PHP imparticular, PHP rookies make the following mistakes:

  • no input validation
  • reliance on magic quotes
  • utilize include files with intermingled display, data access, and business logic (rather than using the MVC model for instance)

I suggest to take a look at the symfony framework.

It has a clean OOP model (and PHP 5 required for it, they do not support PHP 4 with its pre-OOP - it's a real benefit) and clean MVC architecture. From my point of view it's a very well-designed framework. I've had some experience with all CMSes from your list and currently I'm a C# developer and I can say that I've took some ideas from the symfony.

I'm sure that there is no any specific for good PHP programming, standing out from good programming in other languages. Just good separation of concerns and well-designed from architectural point of view - that's what makes a great systems.

But even with great design you can't make PHP works good in large projects. Mostly because this language is dynamically typed and therefore is no any tools that will give you such a great support as Visual Studio for C# or Eclipse for Java. So if you plan to make a big project with long support terms in PHP, I think its better to switch to something else.


I agree with maxnk that Symfony "is an example of a PHP project in which the programming is first rate." The code is well decoupled and Agile practices such as DRY have always been utilized. Yahoo has also had their hand in the project for about 3 years and run Bookmarks, Delicious, and Answers in Symfony.





If you want a good example of project that supports both Version 4 and 5, i suggest that you look at CakePHP, more specifically version 1.2.

The source code is so well put together that you can use it as documentation, when doing development.

There is clear inheritance hierarchy, the variables and function names are sensibly named. I also think its a good simplified example of the ActiveRecord pattern, and for a newbie (myself), helps you better understand how to develop and use a database abstraction layer.

Last but not least, MVC. There is almost no project that you can build in PHP that won't benefit from adopting this pattern, it is one the most natural pattern for web development.


Having refactored a couple major PHP applications, I have found that the Bad Smells listed in Fowler's "Refactoring" book is pretty reliable. Those questions deal directly with your question, and I recommend it for that purpose.


I'd make two points on this, one a positive example and the other, negative.

One for the Negative. If you see SQL anywhere near a Controller or View component, then run away! This advice applies whether using a formal MVC framework or any other architecture, you should never have SQL interleaved with presentation or logic. This is a specific negative example of Separation of Concerns (mentioned above by pcdinh and others) and typical of projects such as osCommerce.

While we owe a debt of gratitude to the path blazed by osCommerce and others, anyone responsible for maintaining the spaghetti code of logic, sql and presentation will still have the scars.

One for the Positive. The use of the Observer pattern (one of the brilliant "DP" Design Patterns referenced by Alan Storm) demonstrates genuine maturity on the part of the architects and a willingness to allow developers on the framework to extend the architecture without compromising the core infrastructure. Magento's Observers and Wordpress' event hooks are two excellent examples.


A project, no matter how large it might one day become, must always start small.

You start with 1 or 2 files and see how they interact. You work out the details, comment it and optimize it. Then you take the next couple of files and repeat.

The hard part is that you'll end up adding more code to older files, which might affect the results received by all the new files.

That's where things go wrong and it's the very reason you need good documentation and you seriously need to think each and every method through before finally implementing it.


@LeDorfier, thanks for the answer. Here's a list of the Bad Smells and a link to a summary.

  • Duplicated Code
  • Long Method
  • Large Class
  • Long Parameter List
  • Divergent Change
  • Shotgun Surgery
  • Feature Envy
  • Data Clumps
  • Primitive Obsession
  • Switch Statements
  • Parallel Inheritance Hierarchies
  • Lazy Class
  • Speculative Generality
  • Temporary Field
  • Message Chains
  • Middle Man
  • Inappropriate Intimacy
  • Alternative Classes with Different Interfaces
  • Incomplete Library Class
  • Data Class
  • Refused Bequest
  • Comments

@LeDorfier, where have you seen code that does this?

I'm marking this CW because I'm not really adding anything...


Tony Hoare once said that programming in BASIC is like trying to do long division with roman numerals.

I think this also applies to PHP for large projects.