Whenever I revert to coding in PHP after an extended sprint in Java, I inevitably find myself somewhat frustrated by some of the shortcomings of PHP. Most of these have to do with how much less maintainable a large code-base becomes in PHP as opposed to Java. Recently, I wanted to quickly whip up a few webapps and decided to try working with the Symony PHP framework to see how quickly I could generate a webapp compared to using Struts 2 in Java.
I found Symfony to be a very effective and well-thought out framework. It was easy to learn and yes, it was not long before I had a functional web app online.
My issues are not with Symfony, but with PHP itself. First off is the plethora of static functions that PHP has on offer. Most of these would be encapsulated as methods of objects in Java (for instance instead of string.equals(otherString) in Java one would use in PHP the static method str_cmp(string, otherString)). This lack of encapsulation and the huge number of global methods makes for a very messy and expansive domain in which to work. Ultimately, I suppose this is a vestige of PHP’s procedural roots.
The lack of namespaces is another gripe, which further adds to the murky domain space of a PHP project. It’s not easy to see how objects relate to each other in terms of an organisational hierarchy in PHP, whereas this is a necessity in Java.
I also find awkward that in PHP you are in the postion of having to import files in order to gain additional functionality rather than importing classes. While technically these two things might not be so different, conceptually they are. An imported file can have functions and variables defined with no association to an object that effectively become global in scope on import. Static classes can have the same effect in Java, but I think most Java programmers have by now learned to avoid using static objects for the most part. The default behavior in Java is to use instances of classes whereas in PHP the default behaviour for many still seems to be to use collections of functions and function libraries.
I also find that PHP feels really quite verbose. All my PHP templates look rather ugly with echo statements littered everywhere. It’s not nearly as elegant as Freemarker.
With all other factors such as scalability, support, and resilience aside, I think PHP works great for small- to mid-sized web apps where maintainability of the code base is not a top priority. But for large, complex, long-life webapps, It seems to me that using a Java framework may be more appropriate in no small part because the quality and maintainability of the code will be considerably higher. This is not to say that you can’t create quality, maintainable apps in PHP, but that it is easier and more natural to do so in Java, where types are static, code encapsulation is paramount, and namespaces rule the roost.