tags:

views:

1264

answers:

11

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...

+4  A: 

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.

maxnk
PHP4 isn't pre-OOP.
stesch
Well, may be you can call it OOP, but from the PHP5 view it is a really pre-OOP. And I'm not saying about C# or Java.
maxnk
Hi Maxnk, nice answer. Note, a lot of people like dynamically typed languages. I'm not one of them, though...
Yar
Thanks, Daniel :)I like them too (perl, ruby, js with jQuery, php) and having fun with them, but I'm trying to use them for quick projects, because I know that it's can be hard to maintain big systems built on dynamic langs. And there are some places where I'd better choose dynamic over static.
maxnk
So I'm trying to use each thing to its intended purpose :)
maxnk
I agree, Maxnk, and I have seen all the IDEs for PHP and they all SUCK compared to Eclipse for Java. And the reason is that you can't make an IDE if you don't have types... you're just randomly guessing stuff, which is what NetBeans/PDT/Altana do...
Yar
A: 

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.

frankodwyer
hee hee hee! This echoes the answer I was going to give, which was "I don't know, I've never seen well-engineered PHP code" ;-)
Steven A. Lowe
I considered, "It isn't written in PHP," but decided against posting it. :)
Craig Stuntz
I'll vote this up if you explain WHY you think this. what don't you have in PHP? Libraries?
Yar
you don't have anything in the language to encourage you to decouple presentation from business logic, and too much that encourages you not to. Yes, I know it can be done. Some people manage to travel safely from A to B at 200mph on a motorbike too. PHP's real strength is in how well it scales down.
frankodwyer
Perhaps you ought to have a chat with Yahoo!, Lufthansa and others (e.g. http://www.zend.com/en/company/customers/) on PHP scalability...
Jonathan Day
+32  A: 

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.

Alan Storm
That is an excellent answer.
troelskn
This is a beautiful answer and hits a lot of great points... thank you for your contribution.
Yar
+1 I just realized that I'm missing an "add-this-answer-to-favorites" button on SO.
soulmerge
I was going to answer that there is no such thing as well-engineered PHP code (at least on the scale the OP is specifing), but this answer gets the point much better.
David
+4  A: 

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.

http://www.symfony-project.org/blog/2006/10/28/yahoo-bookmarks-uses-symfony

http://www.symfony-project.org/blog/2008/05/08/yahoo-answers-powered-by-symfony

http://www.symfony-project.org/blog/2007/10/02/delicious-preview-built-with-symfony

bmeynell
+2  A: 

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.

Jonathan
I'll check them out, thanks!
Yar
What framework would your recommend for MVC in PHP?
Yar
@Daniel Rosenstark I have heard good things about CodeIgniter lately. I'm writing a PHP MVC framework based heavily on Spring Framework called Halo (http://halo-php.org/). Also see http://www.phpwact.org/php/mvc_frameworks for a big list of PHP MVC Frameworks.
Beau Simensen
+7  A: 

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)
Cory House
Yes! Separation of concerns is a huge problem, and the most "natural" thing is just to put anything anywhere...
Yar
+1  A: 

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.

Vordreller
Most PHP projects start small and the stuff works, but there is no structure to it.
Yar
+9  A: 

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

pcdinh
Great answer. It's missing 1) where do you get your code conventions, 2) What about Joomla/WP that you don't like (but that wasn't in the question). But I'll mark you best answer ALSO because you have only one point at this time :). Thanks!
Yar
Zend has it's own coding principles...http://framework.zend.com/manual/en/coding-standard.html
alex
+2  A: 

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.

le dorfier
I voted this +1 in December when you wrote it. Thanks.... http://en.wikipedia.org/wiki/Code_smell
Yar
+1  A: 

@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...

Yar
+2  A: 

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.

Jonathan Day