views:

207

answers:

4

In developing a web application that will handle credit cards, purchases, digital downloads, is there any real world benefit to using Java over PHP?

This will be a store that sells digital downloads.

+1  A: 

There's nothing you can't do in one that you can do in the other. Pick whichever one you're most familiar with. Thats far more important than any alleged technical advantages. The less important differences are:

  • PHP hosting is typically cheaper;
  • Java handles multithreading much better;
  • You end up writing a lot of boilerplate code in Java for stuff PHP has API fucntions for;
  • PHP development tends to be smoother just because there are no build and deploy steps. You click save and reload your browser;
  • Java has probably way more frameworks and libraries than PHP does;

Edit: Are you really arguing Java doesn't have a lot of boilerplate code? Seriously? I'll give you one example: reading a file into a string.

Java:

public String readFile(String fileName) {
  StringBuilder s = new StringBuilder(4096);
  BufferedReader in = null;
  try {
    in = new BufferedReader(new FileInputStream(file));
    char buf[] = new char[4096];
    while ((numRead = in.read(buf)) != -1) {
       String chunk= String.valueOf(buf, 0, numRead);
       s.append(chunk);
    }
  } catch (IOException e) {
    // handle
  } finally {
    if (in != null) { try { in.close(); } catch (Exception e) { } }
  }
  return s.toString();
}

PHP:

$s = file_get_contents("filename.txt");

Beyond that, look at the boilerplate hashCode(), equals(), toString() and getter/setter copying code that ends up in most Java Web applications.

cletus
Boilerplate - like what, for instance?
Thorbjørn Ravn Andersen
I think it depends on what Java technologies you're using. If you're stuck using the old EJB 2.0, there's lots of boilerplate. If you're using newer approaches, not so much. (One thing I'd love to see in Java is automatic JavaBean property getter/setter generation, like what Scala and Ruby provide.)
Jim Ferrans
to read in a file into a string in java: IOUtils.toString(new FileInputStream(new File(fileName,"UTF-8"))); of course your code is boilerplate if u dont know the most important toolsets. (here apache commons IO utils from IO package)
Chris
"You end up writing a lot of boilerplate code in Java for stuff PHP has API fucntions for". If you look, so does Java - your "boilerplate" example included (via apache commons, or even your own utility libraries...). You are using a StringBuilder, a BufferedReader, and a FileInputStream, each of which can be swapped out for different purpose - try doing that with "file_get_contents"...
Matt
Normally you want to replace certain places in your files with recipient specific fields. Do PHP support reading files with "Dear ${FIRSTNAME} ${LASTNAME]" and have them automatically expanded?
Thorbjørn Ravn Andersen
Writing that sort of template is pretty easy with PHP (or you can use something like Smarty). It's pretty easy in Java too (or you can use something like Velocity).
cletus
@Matt: I'm perfectly aware of Jakarta Commons but as a rule I never add dependency on a jar just for one function. I'll use Spring as a given but Jakarta Commons? I an't remember the last time I used it.
cletus
@cletus re:the templating scenario - Thorbjørn is pointing out the benefits of a buffered stream reader - where the substitution is occuring as you read the stream, instead of reading it all into memory and then performing the substitution as you would with php file_get_contents.
Matt
@Matt: Smarty templates are buffered just like any PHP script is buffered (unless you override the behaviour).
cletus
+3  A: 

Java has tons of enterprise-grade support, a lot more than PHP has. You have distributed transactions, lots of database drivers, message queues, persistence managers like Hibernate and iBatis, the Spring Framework, templating engines, web services support, and on and on. There are a lot of high-performance application servers like Glassfish, JBoss and WebLogic. So you should consider Java over PHP in my opinion, unless your application is small-scale has small-scale e-commerce needs and you already feel comfortable with PHP.

For what it's worth, this was advice given to me last year by the VP of Engineering at a large North American e-commerce site.

Update: Java also wins on raw performance, about 30x faster than PHP according to these benchmarks (which are probably not using opcode caching, see comments).

Update: A take on Java from highscalability.com. It emphasizes the fact that Java the language is less important than Java the platform and Java the community. Lots of mature tools to choose from, a lot of people working on it. Even lots of spiffy new JVM languages that you can use along with Java (Scala, JRuby, Groovy, Clojure, Jython and so on). Examples of large sites using Java fully or partially include Fotolog, Amazon, E-Bay, Flickr, LinkedIn, GoogleTalk.

Jim Ferrans
-1 Small scale like... Wikipedia, Flickr, Facebook or Yahoo (all PHP)? This is terrible (and baseless) advice.
cletus
@cletus: Very true, and Cal Henderson's Building Scalable Web Sites shows how it's done with PHP. I was focusing more on "small scale" e-commerce needs, a poor choice of words.
Jim Ferrans
As far as databse drivers go, that's a nonissue. But for performance, was PHP bytecode cached or run fully interpreted?
Chris
Betcha an opcode cache (which anyone using PHP should have set up) would change those PHP benchmarks.
ceejayoz
igouy
@Jim Ferrans - Is that raw performance on web apps? http://talks.php.net/show/drupal08/7
igouy
@igouy, no, these are much simpler benchmarks. I agree that someone in the PHP community should provide alioth.debian.org with a fairer set of tests if the current ones are lacking. Also agree with the presentation that a lot of web site optimization involves things like front end optimization and caching tiers. But sometimes raw performance is a key factor, while at other times you mainly want a productive language and framework. Like so much else, it all depends. I happen to like PHP too.
Jim Ferrans
+4  A: 

Browsers do not care if the HTML they see was generated by PHP or Java, so the differences are pure backend.

You should generally go with the language you know already, as the amount of work needed to learn another software platform "well enough" e.g. Java with any given web framework will be rather high. It is up to you to decide if there is enough code needing to be written to get your finished product to warrant even thinking about it.

My personal opinion is that Java is the stuff used to build cathedrals. Takes quite a while to build, but they stand for centuries. If that is what you need, then go for Java (and learn it well). If not, then choose the language you know the best.

Thorbjørn Ravn Andersen
+3  A: 

I built a e-commerce application in nine months (www.perqworks.com) with the PHP framework Symfony (symfony-project.org) and the ORM Doctrine knowing very little about server-side code or database design. Why I believe PHP was the right choice is:

  1. I could learn PHP code quickly at php.net
  2. Symfony documentation and forums were very helpful
  3. Being a virgin with SQL, Doctrine made sense and gave me some confidence that my queries were well written
  4. The LAMP stack cost is unbeatable
  5. It is hard to sift through J2EE, Beans, Spring, JMV, and the several other problem domains Java fills

Now that the application is written and I have some hindsight, I wonder what the end product and experience developing the app would have been with Django, Rails, or Java. What I have concluded is that the difference between the major web development languages is not the sum of their small differences, but the skill at the programmer to write smart, efficient code.

Christopher Altman