Compare Java, C++ and PHP class features

Java, C++ and PHP all support similar OOP concepts like class/object. Sometimes it can be frustrating, however, for programmers to apply similar OOP thinking into different languages. The reason is that there are some subtle differences between them in terms of OOP design, besides obvious language syntax difference. By doing the apple-to-apple comparison, one can remember those features much better. Here I am starting to compile the list of OOP features that are different between Java, C++ and PHP. Hopefully this list can be more comprehensive as time goes by.

1. Class method

1.1 Method overloading
Java and C++ class can have multiple methods sharing the same name but with different parameter signatures, i.e., method overloading. Unfortunately, PHP can’t.

A consequence is that Java and C++ throws error when calling a class method without matching its parameter signature, while PHP will just take it happily without warning. Such loose behavior of PHP can surprise Java programmers. For example, the following PHP code runs just fine.

class MyTest
{
  public function display($id)
  {
    return $id;
  }
}
$t = new MyTest();
echo $t->display("hello", "world");

1.2 Default parameter
PHP, C++ support default parameter values in method, while Java doesn’t. Java programmers may want to use the Builder Design Pattern instead.

1.3 Static method
While Java can call a static class method using either a class name or an object instance, PHP and C++ have to use the official class name ONLY.
Another minor syntactic difference is that, Java use “.” while PHP and C++ use “::” as the delimiter.

PHP const and static variables

In addition to that const variables can’t be changed, another difference between const and static is that some variables like arrays are not allowed in class constants, so

class mytest
{
public static $c = array(1=> 'hello', 2=>'world');
}

works, but

class mytest
{
const c = array(1=> 'hello', 2=>'world');
}

won’t.

Strange Hadoop error caused by the CDPATH environment variable

When I tried to run hadoop in my Linux box, I got a strange error:

bin/hadoop: line 53: /home/hzhang/tools/hadoop-0.20.203/bin
/home/hzhang/tools/hadoop-0.20.203/bin/hadoop-config.sh: No such file or directory

It said the hadoop-config.sh is missing, but clearly it’s there in my machine. After hair-scratching for quite a bit, I found out that it’s the line before 53 that caused error

bin=`dirname "$0"`
bin=`cd "$bin"; pwd`

The reason is that I activated CDPATH in my shell, and the $bin variable therefore contains the same path repeated twice. All I have to do is turn it off by the following command:
$ unset CDPATH

How can I keep from singing? (Enya)

My life goes on in endless song
above earth’s lamentations,
I hear the real, though far-off hymn
that hails a new creation.

Through all the tumult and the strife
I hear it’s music ringing,
It sounds an echo in my soul.
How can I keep from singing?

While though the tempest loudly roars,
I hear the truth, it liveth.
And though the darkness ’round me close,
songs in the night it giveth.

No storm can shake my inmost calm,
while to that rock I’m clinging.
Since love is lord of heaven and earth
how can I keep from singing?

When tyrants tremble in their fear
and hear their death knell ringing,
when friends rejoice both far and near
how can I keep from singing?

In prison cell and dungeon vile
our thoughts to them are winging,
when friends by shame are undefiled
how can I keep from singing?