top | item 6959115

High Quality PHP

56 points| stedaniels | 12 years ago |acquia.com | reply

94 comments

order
[+] programminggeek|12 years ago|reply
It is really easy to talk smack about PHP because as a language it is not the prettiest, best designed, or cleanest language out there. It often leads to some very gross code.

However, programmers write ugly code in every language and even in a much more beautiful language like Ruby, there are so many projects that end up with ugly codebases, poor performance, etc. that PHP by no means holds an exclusive ownership of terrible coding practices.

Writing high quality code in any language takes significant effort on an ongoing basis. Practices like TDD, code review, pairing, etc. can help, but only if they are applied with significant effort.

[+] maw|12 years ago|reply
You can trot out the trope about how you can write good code or bad code in any language as many times as you wish but it will not change the fact that php makes it unnecessarily difficult to write good code and, through both the language's and its standard library's misfeatures as well as the surrounding culture of sloppiness, encourages writing bad code.
[+] evilduck|12 years ago|reply
This argument makes no sense. You're conflating people and behavior with tools. Bad tools make it easier to do a bad job in any field of work. Novices with good tools will still do a bad job. This is a weak justification for promoting bad tools.
[+] UK-AL|12 years ago|reply
That's true. But why make it worse by choosing a bad language. Surely you have good practice and a language that makes it easier.
[+] cookiecaper|12 years ago|reply
Beauty is subjective. I don't think Ruby is beautiful, I much prefer Python.

The difference is that PHP was not really designed for the type of use it gets. It began as one guy's hobby project and somehow snowballed to power some of the biggest sites on the internet without really having the backing necessary to support that position. PHP maintainers have been desperately attempting to backport the type of functionality that Serious Business people need, but it's never going to be as organized or clean as something that was conceived to serve that purpose.

If you care about code quality, choosing PHP already puts you at a disadvantage. It's not that people can't write adequate PHP code, or that people can't write ugly code in other languages. It's just that most other languages give you a pretty healthy head start over PHP when it comes to cohesion of organization, presence of important language features, etc.

[+] beberlei|12 years ago|reply
Author here. I wrote this article as a guestblogger for Acquia, which is the company behind Drupal. I don't work at Acquia.

The article obviously uses Drupal as an example, PHPBB and Wordpress as comparison because they are all in the off-the-shelf, customizable category of PHP applications.

Both Drupal and phpBB (contrast to Wordpress) have massively modernized their code bases in the last years and improved the quality of their code. Coming from their original level, this is obviously a huge undertaking and still far away from being "high quality code" as an absolute measure. However their efforts in this space are remarkable.

Given that they have so huge installed code-basis that they have to take backwards compatibility seriously (which is also something that contributes to quality of software), explaining why it takes so long to improve the quality of truly old legacy software.

Compare this with many companies that use PHP, that still don't know what automated testing and quality assurance is. The article tries to highlight how PHP open source software use tools for quality assurance, what tools exist and that you should use them in your PHP projects.

[+] SkyMarshal|12 years ago|reply
Thanks for putting all this together. I recently find myself working with PHP again after last looking at it in 2003, and have been digging around getting up to speed.

All sorts of interesting things happening in this space, especially PHP-FPM and HHVM. The language design and code quality/organization capabilities may not yet be up to snuff with Python and Ruby, but the runtime options are as good or better.

[+] WiseWeasel|12 years ago|reply
FYI, there's currently a typo in the first paragraph ('loosing' => 'losing',) which made me cringe; you have to bury the typos further down where they'll have less influence on my perception of the piece.

[edit]It's since been fixed. That was an impressively quick response.[/edit]

[+] polysics|12 years ago|reply
I left PHP about 3 years ago, and I have recently had to look into it again due to a new course I am preparing to teach. The PHP I knew is no more, and it is leaps and bounds ahead of what it used to be. But using Drupal as an example is really a bad choice. Talk about Laravel, Composer, new language features, but not crappy giant codebases.
[+] girvo|12 years ago|reply
It's great isn't it? Variadic functions is about to land, as is splats, and some other neat features like "use function", which will mean a simple module setup with namespacing without classes. Oh and phpdbg built in!

I went to leave PHP this year, and didn't end up doing it, because of how much has changed lately. It's a good language, with awesome tooling know. I can write well-tested, easily readable, maintainable code using small composable libraries, and big frameworks when needed, without fighting against the dumb parts of the language. It makes me happy :)

[+] jiggy2011|12 years ago|reply
The big difference that the PHP ecosystem seems to have vs the ruby/python ecosystems is an emphasis on providing complete stand-alone solutions to problems vs providing developers with the most flexible and powerful tooling to develop their own solutions.

Whenever I find myself wanting a photo-gallery, an eCommerce platform or a forum I always seem to find that the most mature solutions are based on PHP.

The advantage of this is that semi-technical webmasters can easily deploy complete websites without writing a line of code, thus the underlying code quality is of secondary importance to the functionality provided.

[+] polysics|12 years ago|reply
That is not necessarily an advantage, considering the same semi-technical people leave their websites available for any kind of security breach. This is not 2001 any more, people should know what they are doing.
[+] nkuttler|12 years ago|reply
I'm speechless that "phpBB, Drupal, Wordpress" are supposed to be the "great" examples of "high quality PHP".
[+] endijs|12 years ago|reply
It's not what is said there. Read again: "The focus of PHP has always been on "getting things done" and the language’s developers still value this goal highly. This practicality has spawned great open-source software such as phpBB, Drupal, Wordpress and various other well known applications that are hosted on millions of servers worldwide" So - "getting things done" have spawned great tools like phpBB, Drupal, Wordpress. They are great, because they address problem for end users and are used by millions. It's not said that they are high quality PHP projects if we look at code level.
[+] beberlei|12 years ago|reply
I mention Wordpress only as great example of "getting this done" approach of PHP. Never did i mention it as example of "high quality PHP code".

phpBB and Drupal have their problems, but they both improved their code-base alot, moving from legacy PHP software to software that is much more maintainable. The road to "high quality software" is long, but throwing software out of the window instead of slowly improving it is not the right solution either.

Both projects have high numbers of unit-tests and can guarantee backwards compatibility and regression free releases in a much better way than most of the software out there in any company, written in any language.

[+] dotBen|12 years ago|reply
Acquia is the commercial company behind the development of Drupal, so understandably they're going emphasize Drupal as a great example of PHP ;)
[+] deckiedan|12 years ago|reply
actually, it's examples of "high quality software", I believe. And, problematic and freaking annoying as those can be at times, they also have been, historically, extremely easy software to set up for enabling people to get stuff done.
[+] thomasfedb|12 years ago|reply
Of course you can write crappy code in any languages, and conversely you can write nice code in most languages (perhaps not in INTERCAL or Whitespace).

Personally I prefer Ruby over PHP for precisely the same reason I don't begin my day by shooting myself in the foot.

[+] mekoka|12 years ago|reply
It still baffles me that whenever an article like this comes up, people still justify choosing PHP because of its ease of setup compared to other languages. Seriously? What are you going to tell me next, that you chose Ruby because the "Hello World" tutorial was 15 minutes shorter, than the Java one? Come on now.

Yes, the fact that PHP is easy to setup may explain its popularity, it doesn't however justify choosing it, considering that you may spend 2 or 3 days setting up a tool that you'll end up using day in and day out for the next couple of years, to do much more difficult things than just greeting the world on a blank web page.

[+] Tloewald|12 years ago|reply
There's a big difference between easy to set up (requires no setup) and a bit harder to setup (requires a different and more expensive ISP and then a bunch of extra effort).

The reason I picked PHP for a project years ago wasn't because it was easier for me to set up, but because it would be easier for users of my code to set up. If you're writing code to work on your servers that's a different thing altogether. Similarly I picked PHP for a later project simply because it was available and configured, whereas anything else would have had to go through approval and tech support.

For my personal stuff I use a shared server (dreamhost) which is pretty typical for low-end providers. PHP isn't merely easier, it's possible.

Now my professional stuff goes on dedicated servers, so that's great. But the problem with them is that when I want to tinker, I want something really simple and foolproof to work with. If I come back after not looking at a side project for several years, I'd like to not have to spend days figuring out the runtime setup.

We are getting close to a better world. I see a lot of pretty cheap/free and convenient options for hosting node and rails projects, but usually database access is yet another stumbling block.

[+] enimodas|12 years ago|reply
Lots of hate for php in this thread, but this has me thinking: does anyone know a better solution to solve my problem? I have to build a CRUD-like webapp for some database, and with a bunch of authentication and authorization. Lots of forms and reports, think MS access. I was planning to use symfony2 and a generator bundle for this, as symfony2 has those things build in and the generator will write a lot of the code i need. I'm also pretty time constrained (2 weeks), but I'm still curious what other options there are out there or how other people would solve this.
[+] fowkswe|12 years ago|reply
I think a valid point to make here is that what has propelled Ruby, Python, JS, etc, forward has not been the language, but the frameworks that have put context and boundaries around using those languages.

Its entirely possible to write a poorly organized bowl of spaghetti using Ruby if you leave Rails out of the picture. I would venture to say the same is true for any programming language.

PHP was born before the idea of an open source framework had been propagated, it spread around the web before many of the paradigms and patterns had evolved to what they are today, and unfortunately it failed to gain acceptance by the elite programmers that have carved a community out using those other languages.

The other languages' respective frameworks have large, organized communities which have attracted very smart people that have put large swaths of functionality in place, before you ever have to write a line of code.

Its playing catch-up now, but there are good examples of PHP out there and it most certainly can be used in an elegant, high-quality manner.

[+] sixthloginorso|12 years ago|reply
> Its entirely possible to write a poorly organized bowl of spaghetti using Ruby if you leave Rails out of the picture. I would venture to say the same is true for any programming language.

You can easily write one with Rails over just a few months of maintenance. I hate, hate having to do a damned autopsy to trace all the @instance (read, global) variables copied and inserted into the objects that you never create yourself, -because Rails devs are apparently above having to endure the savagery of passing arguments to their methods- and having to grep every single function that gets automatically included from everywhere in the project into the namespace.

[+] heron87|12 years ago|reply
I believe "high quality PHP" is an oxymoron. It's just impossible to be aware of all the pitfalls PHP contains. Here is just one example among many:

> if( '33333333333333333333' == '33333333333333333334' ) echo "equal"; Output: "equal"

The "reason" for this very odd behaviour is that the two strings are automatically converted into integers, which leads to an int overflow, which PHP ignores (ignoring errors is very PHP specific) by guessing that the two "numbers" must be equal. I'm sure they even call this a feature...

[+] jiggy2011|12 years ago|reply
I just tested this (PHP 5.4) and I don't get this behaviour. In this example the two are compared as strings not integers so are not equal.

PHP does not automatically convert types unless you do something like this:

if( 33333333333333333333 == '33333333333333333334' )

But here the problem is the integer overflow not specifically the type conversions. You are going to get weird behaviour no matter what you do because you don't have enough bits to store that number.

If you want to use numbers this size then you are going to need some type other than the standard int.