I ran across a Novice java programmer’s mistakes and Common java programming errors page. This is a dubious exercise, but I am going to try and use these pages to compare the learning curve of Java and PHP.
This is inherently unfair, as these are lists of known learning problems in Java that might not exist in PHP. Please tell me, what are the learning hurdles of PHP that might not exist in Java?
First, the Java mistakes:
Naming the Class Differently from its File Name
This is an example of unwanted entanglement: the need to keep two items synchronous across a distance. No such filename entanglements in PHP.
Advantage: +1 PHP
Comparing Strings with ==
Strings aren’t objects in PHP so naive == works as expected, although weak typing can make the results of == more difficult to predict and having a === operator means more things to learn. (see PHP syntax exam)
Advantage: +.5 PHP
Forgetting to Initialize Object Arrays
PHP doesn’t require an array to be declared with a certain type, so this bit of confusion isn’t really possible.
Advantage: +1 PHP
Putting Several Public Classes in One File
Not a limitation in PHP. Naive version works as expected.
Advantage: +1 PHP
Shadowing an Attribute with a Local Variable
Attributes must be prefixed with $this->, so it isn’t possible to shadow an attribute with a local variable, although it is still possible to forget to add the $this->.
Advantage: +1 PHP
Forgetting to call a Superclass Constructors
PHP has this problem as well. Unfortunately, in PHP before __construct, there was an unfortunate naming entanglement between the class name, the constructor, and the superclass name.
Advantage: tie (+1 java vs. PHP 4)
Catching Exceptions Incorrectly
I might be getting this one wrong, but I think both languages have the same problem.
Advantage: tie
Returning void from an Accessor method
The type declaration for return value is unnecessary in PHP. Naive version works as expected.
Advantage: +1 PHP
Calling Instance Methods from main()
This is a variation on the simple hello world argument. The Java hello world makes more demands on the knowledge of the programmer.
Advantage: +1 PHP
Treating Strings as In/Out Parameters
Again, in PHP, strings aren’t objects and the naive version works if you pass by reference.
Advantage: +1 PHP
Declaring a Constructor as a Method
The special __construct name eliminates this confusion.
Advantage: +1 PHP (tie java vs. PHP 4)
Forgetting to Cast Object Data Types
Dynamic typing means no casting means no forgetting.
Advantage: +1 PHP
Extending Interfaces
Dynamic typing in PHP means that interfaces need not be declared and used as much as in Java, but I’ll still call this a tie.
Advantage: tie
Calling Superclass Methods and Forgetting to Use the Return Value
Advantage: tie
Forgetting to Add AWT Components
N/A
Neglecting Import Statements
Java and PHP both ship with large libraries. The built in PHP functions, however, are universally available. However, Woe to the beginner that has to re-compile PHP.
Advantage: +.5 PHP
Forgetting to Start Threads
No threads, no problem.
Advantage: +1 PHP
Using Deprecated java.io.DataInputStream readLine()
N/A
Assigning and passing double literals as floats
I’ll probably get this one wrong, but PHP has only doubles. Thus, no problem. (even if it did have multiple types, I think that dynamic typing would avoid this problem)
Advantage: +1 PHP
Second, the Common (Java) programming errors:
Not specifying the size of a new array.
Arrays grow and shrink in PHP. size specification unnecessary.
Advantage: +1 PHP
Not using correct array bounds.
foreach eliminates this problem. I understand Java has an equivalent now.
Advantage: tie
Doing arithmetic on an instance of a wrapper class.
No wrappers, no problem.
Advantage: +1 PHP
Adding a value of a primitive type to a collection (a set or list) or a map.
Dynamic typing, primitives welcome.
Advantage: +1 PHP
Not casting the value of type Object returned by list.get(i) or map.get(key) to the required type.
Not a problem in PHP. (covered above)
Advantage: +1 PHP
Using static components unnecessarily.
A little nod to the static main here.
Advantage: tie
Not reading the next line inside a loop
N/A
Creating a string tokenizer for a line before checking the line is present
N/A
Reading all input before processing it
N/A
Threading code
No threading, no problem.
Advantage: +1 PHP
Doing nontrivial computation in a class constructor
Advantage: tie
Not using common API methods
Advantage: tie
Not breaking out of a loop when required
Advantage: tie
Not breaking at the end of each case in a switch-statement
Fall through capability in a switch statement isn’t used and useful enough to justify requiring the extra break on each case in the normal course of events. When fall through is used, it is confusing. (see single point of entry argument in php goto) A pox on both houses.
Disadvantage: tie
Assigning constant values to boolean variables in if-statement
In a way, I find the naive version can be a little easier to understand.
Advantage: tie
Declaring variables globally, unnecessarily
Advantage: tie
Repeating code that should be in a method called repeatedly
Advantage: tie
Being too complicated
I’m tempted to knock java in general for this for things like EJB, but I won’t.
Advantage: tie
Combining computation and input/output in a single, complex method
Its easy to mix html output and php. I’ll give this one to Java.
Advantage: +1 Java
I like PHP, but a few of these +1 seem to be -1 to me:
Multiple classes to a file is -1 from my POV when developing. (There are 3 +1 given to PHP for lack of file/class relationship constraints, which seems rather excessive.) In Java there is no question about which file to open to see a class. In PHP it could be anywhere.
“Not casting the value of type Object returned by list.get(i) or map.get(key) to the required type.”
This is no longer an issue in Java 1.5, right?
Coherency of language: -1 for PHP. PHP has some internal stuff that’s in objects, but most in functions. Function naming, parameter orders, and return values exhibit little consistency. Needing to use PHP in OO environment can often reasult in creating wrapper classes for basic procedural API (e.g. DB abstraction, File IO abstraction). Frankly, a mess.
Lack of java-style overloading. PHP -1. The new PHP5 object model introduces signature checking (yay!) but since PHP doesn’t allow overloading a great deal of flexibilty was lost in this move (boo!). No longer can you override a method & provide an incompatible signature. This mixture of strict OO + PHP’s traditional loose typing is really shooting PHP in the foot.
“No threads, no problem.” PHP being unable to run (w/ stability) in a multi-threaded environment would seem to me to be a weakness.
On the exception front Java has a +1. Even though both languages do support exceptions, Java has an advantage with checked exceptions. Java requires that methods which can throw an exception (e.g. by nature of calling other methods that throw exceptions & not using try/catch block) must declare the exceptions they throw. PHP has no such feature, forcing developers using a library to rely on phpdoc documention or to examine the source. Java also supports finally { } which is extremely useful for cleanup.
-Hans
Oh, one more comment. The mention of ‘threading code’ in the java mistakes document is talking about threading program logic, not traditional process threads. Still a -1 for PHP that it is not threadsafe.
-H
Calling PHP not threadsafe really isn’t correct either. Core PHP is and has been threadsafe for quite some time. The problem is that any of the countless C libraries you MAY compile into php may not be. Additionally, in PHP’s share nothing model you will never make the mistake of using threads–hence shooting yourself in the foot when it is time to scale your app across multiple servers. I say, +1 to php.
BTW – I say +100 to Jeff for posting this. Sure, it is admittedly biased…but great fun none the less.
With respect to Hans, I think that the title really says it all: “Why PHP is easier to learn than Java”. Of course Java is more the programmer’s cup-of-tea (coffee?), but that’s because it’s designed to be.
Great post!
Very biased opinions in here, some valid some not so.
Very unbiased opinions in here, some invalid some so.
PHP’s core is not thread-safe. Try using PHP in a threaded environment such as Tomcat without any additional extensions nor libraries. It doesn’t work. It’s not thread-safe. echo() is not thread-safe.
Closing database connections in a try/catch/finally block.
PHP +3
You are right. PHP is easire to learn. Because it is a script based language. And as any scripting language it better be easy to learn.
Some of the disadvantages of java above are actually the advantages. Ans some of the above advantages of PHP are actually yhe reasons why PHP is still used primarily for front end programming and not for core business logic.
Bagus, cuba untuk buat yang terbaik!!!
“No threading, no problem”? That’s the stupidest thing I’ve ever heard!
I’ll write a language that *excels* at adding two numbers together. It’ll be better than ANYTHING PHP can do because “no web support, no problem”, “no variables, no problem”, “no syntax, no problem”!
What about “no programming, no problems”
I know the thread has been dead for a while, but…
Despite PHP being a scripting language, I still found quite a bit of “steepness” in the learning curve:
It doesn’t help when you have to debug/maintain someone else’s code base.
I also think HTML output / logic (emphasis on “slash”) should have given Java more than one point. And forgetting to use $this to access instance methods/variables is still something I do quite alot… (should have been at most a tie).
(I could go on a rant about PHP for a while, but I’ve mostly forgotten most of the head-and-heart-aches.)
What about learning an entirely different webserver for java, horrors like Tomcat, and jars and wars and tons of xml, and that’s just to get a simple webpage going.
Compared to what getting running with apache and php on Redhat in what, a couple of hours?
Hmmmm