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.
You’ve summed my thoughts up exactly!
I’ve been “forced” to program in php for 2 years now, and I’ve hardly ever run into code that isn’t a global loving procedural mess! PHP developers write bad code, and it’s because PHP is too loose…
I’m now trying to work on some Google engine apps in my spare time and retrain myself into using java! PHP is horrible.
You guys are absolutely wrong!
PHP/Java Quality is depends upon their uses in our apps.
Now-a-days PHP is high growthing language that can runs even iPhone apps too.
So both are having them own individual quality.
But in Maintenance concern, Java is preferable for large scale apps.
but PHP can also do it with classes and extending..
We are doing Web Development using .net, PHP, Java, XML languages. Among them
PHP is best of us.
Our Company Website :www.webdesign001.net
We have the namespaces support in PHP some time now
Also About the file including you can use PHP Auto Load to load your classes with out including the files.
In the end there is no or bad programming language. It is all on the developer
Yeah Karthik! I can see that your website is great *sigh*. And what kind of “XML languages” do you use then? The XML language language?
And how yould you use PHP on an iPhone? I guess you mean the website’s output for mobile devices. Well… Whatever…
I also agree with richard: Java is a cool language. The coolest feature is the . operator. I do not like -> ! And the object-orientation-stacked-on-top-of-a-procedural-architecture is in my opinion the biggest problem for PHP. Rewrite it… and then remarket it, thats my advise for ZEND.
Well… here at my company I am forced to use PHP for everything. But I don’t. I started to mix bash with python for systems engineering purposes. And then I was told that Python was gay and PHP was much better. I was like: … the fukk?! Now imagine my supervisor’s reaction when he found out that I wrote services in Java to keep our servers interconnected in case of a system failure. He was not amused! But on the other hand: he cant code!
Peace
hi spunken, i agree with you, if you dont mind can you mail me your company website at ganesh[dot]guwahati[at]gmail.com. I had to write email id like this as i think richardrauser.com dont like or publish email.
[...] this blog post does a good job telling us why Java is better… [...]
I use both languages for different reasons. I do my best to stay away from the Object Oriented features of PHP because they mainly make my life harder when dealing with OO in a scripting language. Imagine trying to see if your code works by having to run the entire app to only find out you incorrectly referenced another objects nonexistent method. With Java, I at least get a compile time check. When I’m dealing with fairly complex business logic, complex business apps, I would go with Java because dealing with OO in Java is easier. If I’m doing a simple small to mid-size app (I use size relative to number of features to be built as opposed to number of users) – i would use PHP. It’s easier to stand up and start running web pages. I don’t have to deal with the challenges of a J2EE application, mainly because those features are not needed for a simple web app. If I do have a large web app (# of features) or a the complicated business process I would go with Java. It’s just easier.
As I know, facebook is running on PHP. My point is , if PHP is only for mid/small type web apps then, how facebook is running using PHP. Perhaps, large scale web apps should be more bigger than facebook.
Actually that’s an interesting point, HK… Facebook does use PHP but several of their developers are on record as saying that their PHP code base is a quagmire, or at least in past it was.. The most important thing about Facebook, though, is that only the front-end is done in PHP. Their messaging system is written in Java and their back-end is in C++.. which proves what Michael and RR were saying about proper OO languages being more appropriate for the heavy lifting, i.e. the business logic.
After reading it seems that the problems is more about structure than a technical problem.
PHP lags specification’s compared to Java.
Java has JSR(Java Specification Request).
example.. JDBC is having a implementation from lots of vendors like.. MySQL , oracle, PostgreSQL etc…
There should be similar standardization in php.
Java is a great language. If you never hope to deliver anything. This “scalability” stuff is to laugh at. Keep toting SUN’s tired old line if you want to. PHP rules the web.
Java: Write Once, Run Anywhere.
PHP: Write Once, Read Never
PHP is a horrible mess. Stay away from it like the plague.
Facebook running php… Uhh…yes and no, Facebook originally ran php when it was small scale, while it still uses php…they also run C++/Java/Perl/Python on the backend. It certainly isn’t all php to run that thing. You can just look at their engineering job posts at FB website and verify this for yourselves.
Well, that’s the good thing about PHP (not a bad thing!) – the including of PHP libs.
This is why it is really easy to implement a run-time plugin/addon functionalities such as Drupal, Joomla etc… offer.
So far I do not know a single Java based website that managed to offer a “plugin” backend so that you can add new features to a live website without having to re-compile or restart the webserver.
In the end, websites are about getting data and iterating over it and rendering templates.
Most Java features are not needed for websites and there is always a workaround.
It is also a queston whether you need X servers or a single server. When you start in Java you need to setup a couple of servers first:. Maven repository, CI server…staging, live..
Java is expensive: A lot of time is wasted getting your environment setup, things working and getting things working again due to updates of dependencies outside of your scope.
I agree, Java is a much more developed programming language. But PHP is more widespread and easier to deploy. Besides the application and its abilities are only as good as the developer who created it.
I’m not sure about any of this. I’ve been writing PHP for years, and I enjoy it immensely. I wouldn’t necessarily agree with any of the negative points you brought up. It is true that a procedural PHP application can become spaghetti in short order, and it is true that the code can become disorganized and hard to follow. But those are qualities that depend on the developer rather than the structure of the language. Sometimes the flexibility of the language can be used in the ugliest ways. But PHP applications can also be quite beautifully organized. Not to mention that some of the biggest websites out there are powered by PHP. I have been using MVC PHP frameworks such as codeigniter and cakePHP, and I find that they make using PHP quite easy to understand for anyone familiar with writing web apps in Java. I believe that either language is perfectly suitable for developing web applications, but I prefer PHP, because it is specifically designed for web. I believe in choosing the correct tool for the job, and for me, in most cases, that tool is PHP.