56 points · by stedaniels · 12y ago · 94 comments
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.
This isn't directed at you. I just didn't realize HN was part of that nerd subculture. I thought HN was for adults.
It is one of the easiest to get started with, most documented, best supported and flexible tools out there for 90% of the things you see on the internet today.
I spent many years doing PHP. I am very glad that those years are behind me. Even if you allow for it being easy to setup - which is debatable - setup only happens once and is not very hard in any language. If you find it hard to install a programming language by following instructions, you are an amateur pretty much by definition.
I mean, many of the endless list of functions imported into the default namespace were named so that their hashing distribution worked well with strlen for a hash. How much work went into working around the fact that just about the worst hashing function that you can come up with in three seconds was used?
And we can enumerate such blunders all day here. There are many better languages with good tooling, and guess what, they're free. I wouldn't use PHP if I can avoid it, and there's ample chance to avoid it nowadays.
In the same universe where munging together logic and display code, or model and view code, is a bad idea. It's the same problem as using classic .ASP, except people are smart enough to not get all indignant when people point out how bad .asp was.
> It is one of the easiest to get started with
That's nothing special. It's trivial to get started with dozens of web frameworks.
> most documented
Most frameworks and languages are well documented. It's like you're bragging that you have the most well-vacuumed car seats, at a car rally.
> best supported
Again, this is nothing special. Most frameworks have IRC channels and mailing lists.
> for 90% of the things you see on the internet today.
90% of the things on the internet are so simple that you could easily use PHP for them, like blogs, simple forums, mostly static sites, etc. That doesn't necessarily make PHP a good tool. It just shows how you can easily do trivial tasks with mediocre tools.
Because it's so easy to get started using. Compare that to almost any other language out there that is a nightmare to set up.
At the end of the day the code if it works it works and PHP just simply works. Not for everything but for most things.
Reminds me of some of my friends who will obsess over the specs on a digital camera (or the power of a particular model of PC or Mac) and completely ignore setting up and composing a good photo. Wanna be purists I guess.
Many years ago I put together some shell scripts using awk, sed and an early shell. The were used to do an estimating system in the business that I was involved in at the time. They were crude and the code was really bad and I had at the time really little clue on how to do things the correct way. Had I had to learn c to do the same thing (which I did somewhat a bit later) I would have never ended up with anything at all. Professionals laughed but the people who bought the business ended up using the system that I wrote for about 4 or 5 years until they found something to replace it. It got the job done. Under the hood what was going on (in this particular case I have to stress as this was pre-net and only used internally) wasn't really a factor.
http://iamjamesblog.wordpress.com/2011/11/14/configure-apach...
Nobody does it because there's just no demand for two crappy default hosting configurations (this isn't even a criticism about PHP the language, it's just a bad model to use for hosting web applications).
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.
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.
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.
[edit]It's since been fixed. That was an impressively quick response.[/edit]
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 :)
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.
Edit: What's even worse is that we don't get examples of PHP code that was actually written with proper development procedures from the ground up. I mean, I was genuinely hooked by the headline..
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.
Personally I prefer Ruby over PHP for precisely the same reason I don't begin my day by shooting myself in the foot.
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.
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.
That said, for your use case (since you're thinking of symfony2, it's clear that performance isn't an issue/concern: http://www.techempower.com/benchmarks/#section=data-r8&hw=i7...) I think Django is almost certainly a superior alternative. It comes with a built-in authentication and group/role-based authorization system, a built-in 'admin' system that can be used to serve up basic CRUD forms for your models with minimal effort, and the ability to code custom 'reports' as add-ins to the built-in admin system with only minimal additional effort. If your models aren't terribly complex you could have a test-ready basic CRUD app matching your description in less than 30 minutes, 90% or more of the code for which will have been generated (or come pre-packaged) for you.
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.
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.
> 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...
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.
See the string comparison routine in 5.3 [3] and 5.4 [4]. The new implementation still inspects the strings to see if they are numeric and then tries to do a numeric equality check rather than string equality check, leading to amusing tricks like
var_dump("0xFF" == "255.0"); // true!
But if both values overflow the long type and their double representations are equal, the new implementation falls back to actual string equality. A quick check at [5] shows that both values heron87 compared have identical double representations, assuming (sizeof double == 8), so the new representation detects the long overflow, then it sees that double representations are also identical, and then it returns to string equality rather than numeric equality. The old implementation did not consider long overflow and ended up comparing the numeric values.[1] https://bugs.php.net/bug.php?id=54547
[2] https://bugs.php.net/bug.php?id=62097
[3] https://github.com/php/php-src/blob/PHP-5.3/Zend/zend_operat...
[4] https://github.com/php/php-src/blob/5.4/Zend/zend_operators....