Is Java better than PHP?

at 2004-12-16 in Further reading by friebe (0 comments)

Inspired by John's blog entry Is PHP better than Java?, Alex and me got into a talk yesterday evening. What is better? The answer is: Whatever suits your needs best.

Let's do some analysis of the current situation: Why exactly is there all this talk about PHP vs. Java? PHP is not Java, people keep on saying - they shouldn't be comparing it then, all the time. On the other hand, they feel it needs to compete with it in the enterprise market (I've said the "E"-word!;)). And that's why people try to defend PHP, to create "language shootouts", to bring the discussion down to "what do you mean by enterprise ready", to bash Java on its shortcomings, to say "yes, we are enterprise ready". But does it work? Why is there still widespread belief that Java is enterprise ready (which it is) and why do CEOs and CTOs not have the same kind of confidence in PHP?

Partly and mostly because Sun can market their programming language as "enterprise ready": It offers a solid core library which is well-designed, consistent and extensible ("...and it's been called Enterprise Edition for a long long time"). Partly because of Sun Inc. itself ("they look reliable, a big company, sell pricey hardware also, heck, they must be trustworthy"). Partly because it is object oriented and therefore requires more theoretical knowledge to learn ("those Java people are geniuses, they have all kinds of super duper words and abbreviations, it must take years to learn all of them").

Now look at PHP: Developed by an open-source community ("long-haired students working in their free time"), with hardly any marketing ("I didn't know PHP could be used to write an application server"), with a reputation for hackish code ("hey, even I developed a guestbook in PHP once") and a functionally oriented and chaotic base library ("I can never remember the function names, was it str_rev, strreplace, and what is the argument order of in_array?").

Well, these are prejudices, of course, but that's the way it works for the management board. Sun has succeeded in selling Java well there, PHP hasn't had the same amount of attention and surely does not get the same amount of - well - respect.

Back to the original topic: What does suit you best?

Well, let's first have a look at the really good parts of Java:


What can PHP hold against that?
  • The easiness of development: Change your sourcecode, save it, reload the page or execute your shell program again. No compiling orgies involved. No Tomcat restarting needed.
  • Efficient string and array functions
  • The excellent online manual
  • It is easy to learn (partly due to the afforementioned manual).

So what is PHP missing to be able to really compete with Java?
  • Namespaces
    There will never be a comparable amount of frameworks and libraries that fit together as well as they do in the Java world if there are no namespaces. Developers as well as users will always run into name clashes when trying to combine, say, the power of Propel with another similar collection of classes, you'd inevitably run into problems with classes having the same name. And what users are not willing to do is to patch sourcecode to make it work.
  • Ease of extensibility
    Extending PHP with functionality which goes beyond what one can do with userland sourcecode is a pain in the ass. Only people who know C well, have enough spare time and nerves to fight theirselves through tons of undocumented code and find out how to compile an extension on different systems will be able to do so.

You can see I haven't mentioned a framework (to compete with the Java standard classes) in this list. Although I think that it is important that there are frameworks for PHP, I do not think that the PHP group should go out of its way to create one. I (with all due respect to Marcus) do not think that the effort made in this direction, SPL (the Standard PHP Library), is any good: There is no concept of where it will go, no RFC, no community involved, it pollutes the global namespace with classes you cannot get rid of even you want to (except for compiling PHP with --disable-spl) and it consumes memory even if you don't use all of it (you know, in userland, we only include() sourcecode we actually need...). Plus, it is written in C, given that PHP userland itself is to some means not powerful enough to create such functionality (which is a bad thing). Being written in C makes it fast but also more error prone and less extensible. To put it short: Frameworks are nothing the language itself should provide - it should provide the means with which you can write one.

So, taking the following steps:
  • Add namespaces
  • Make PHP more robust - change fatal errors to exceptions where possible. Nobody needs an application server stopping hard when NULL->method() is invoked.
  • Create standards and define how the language should work instead of hiding this in .phpt-files and mailing list "descisions"
  • Create a portal page on php.net (a real one, not just a page containing release notes and conference announcements like currently available)
  • Integrate the other available resources such as Planet PHP and "PHP companies" (such as Think PHP) into it
  • Go away from the "PHP is for the web" idea. PHP needs to be more multi-purpose - take a look at businesses, for instance: What do they use Java for? Why don't they use PHP for it? Hint: Application servers, threaded backends for monitoring purposes, desktop applications...
  • Listen to what users have to say (if you've read the PHP developer's mailing list, you know what I mean). Listen to what high-end developers have to say and do not tell them "PHP wasn't made for this".
  • Make extending PHP easier - by means of writing C sourcecode (JNI could serve as an idea or Wez' ffi) and by enabling userland sourcecode to get down deeper into the guts of the language.
  • Create language syntaxes that may seem unnecessary at first (annotations, e.g.) but open doors to creative people (details)

This should get PHP going in the right direction. If PHP wants to compete with Java, it will also have to look at the good things that exist "over there" and learn to embrace ideals besides "web only" (I know about PHP-GTK, but that's still a niche product) and "PHP is not Java/C#". Things that seam beaurocratic and academic in the beginning like RFC-based development will add a touch of professionalism. A portal site adds transparency and provides information to decision makers in IT projects originating from the PHP project itself and not from third-party sources, giving it an authentic touch and making them feel more comfortable. The added flexibility in terms of userland access to internal functions will enable developers to create powerful solutions previously not available (think about AOP, a sandbox, a debugger or hotswap functionality - those would be feasible in C, but not in PHP userland at the moment).

Given the afforementioned prerequisites, I think PHP would not only suit more people better but it's acceptance level would also increase where it really matters - the descision maker's desks.

It's not enough for us developers to just love the language:)



Subscribe

You can subscribe to the XP framework's news by using RSS syndication.


Categories

News
General
PHP5
Announcements
RFCs
Further reading
Examples
Editorial
EASC
Experiments
Unittests
Databases
5.8-SERIES
Unicode
Language
5.9-SERIES

Related

Find related articles by a search for «Is».