Today I’ve read article “Defending PHP” by Jim R. Wilson. He begins saying Ugh. I am so tired of defending PHP. And I’m saying “I am so tired of people defending PHP”. Why? First of all, if everything is OK, the language defends itself, and if lot of people complain about it, maybe really something is wrong with PHP?
I’ve been working with PHP since 1999, so I think it’s quite a bit. I’ve done personal, small and really big projects with it. And what I can say, is that PHP suck in general, but it’s great to develop quick and ugly code. It’s not my personally favorite language, but I’m still using it, and I even run few community projects in Poland (I’m leader of Polish PHP Manual translation, PHP FAQ etc). I don’t like it, but it’s useful in some part of programming ecosystem.
I’ve said ugly – projects written in PHP won’t be pretty, and if they are – it wasn’t worth of it. Why? Because applying advanced (well, advanced for average PHP programmer) techniques like design patterns is an overkill for most of PHP projects – it’s like using design patterns for Hello World. For small to middle projects there’s no need for custom-made OOP, apart from common libraries. Building web sites is generally linear problem: parse input data, load template, load data from database, put data into template, display template – it’s how 99% of page code looks like, so there’s no place for impressive OOP (exception are utility libraries). I’m intentionally skipping wonders like PHP-GTK, because I’ve seen few projects trying to use it and I’ve seen how they were abandoned (maybe something changed recently about PHP-GTK stability and API consistency, but I haven’t heard about it).
I’m not against PHP, OOP, and OOP in PHP. All of these have to be used with proper consideration, and criticism and defense of it has to be spoken with proper understanding of context PHP works in. Also, people criticizing any feature have to forget about personal preferences, or even experiences. Why is that? For instance, Jim Wilson in his article says that “PHP doesn’t have namespaces” is an irrelevant complaint and solution for this would be using classes as wrappers for functions. It’s obvious sign of lack of experience with bigger projects, where namespaces can save a lot of trouble with class conflicts. Wilson also states some PHP features (like existance of eval) as advantages, whereas most of the experienced programmers regard this as threat and suggest avoiding it if possible. I’m not saying that eval is a sign of PHP sucking, but treating this as an advantage is improper.
People have to remember that some things have two sides, like built-in features of PHP. Yes, it’s great that PHP supports many databases, can generate images and tie your necktie, but you have to pay a price for including that many extensions. Memory usage and interpreter complexity are the obvious ones, but there are more. First thing that struck me was very annoying inconsistency in API of similar extensions. Different naming conventions and different order of parameters to name just few. PHP interpreter developers seem like they don’t have idea for leading a big project, or they don’t have one vision of rules, or they don’t know how to enforce them. Now it’s too late for some changes, as it’d make terrible fuss among PHP programmers, just like it was with register_globals some time ago
Argument between dynamic and static typing in most articles is presented like the matter of preference – “dynamic typing is good because it’s easy and I like it” or “static typing is good because it’s easier to find error and I like it”. I won’t express my preference. I’ll only say that dynamic typing should be implemented with great cautiousness, and should be followed by education of novice programmers. Weak typing implementation in PHP promotes errors like 1 == "1foo", what is obvious to experienced developers, but difficult to understand to beginners, which are most of PHP programmers.
Done with criticizing, now praising. As I’ve said before, PHP is great for rapid development, and probably it is the main factor why people are choosing it. It’s easy to learn, it’s easy to code, there are many really advanced applications (both commercial and free), community is helpful etc.
Question is, do you really want to stick with quick and ugly solutions? I’m not saying that it’s wrong. If it suits your needs – why not? Problem is when your application is growing – both in the matter of users and modules. Scalability usually is not the main design feature in the beginning and developers end up with rewriting whole application after some time.
To summarize: PHP is good tool, but only if you know and accept its faults and disadvantages, and you know where it could be applicable. Arguments like “someone has used PHP in space shuttle” are not real arguments – because something exists does not mean that it’s good and it works properly.