Backwards compatible PHP constructors

I reasently discoverede that the backwards compabilities in PHP is even more thorough than I expected. When a class extends an old-style PHP class using old-style constructor (constructor has the same name as the class) you can call the old class’ constructor using the new __construct method.

Example:


#!/usr/bin/env php
name = $name;
}
}

class fresh extends old {

public $greeting;

public function __construct($name) {
parent::__construct($name);
$this->greeting = 'Greetings ' . $this->name;
}
}

class faulty extends old {

public $greeting;

public function faulty($name) {
$this->old($name);
$this->greeting = 'Hey ' . $this->name;
}
}

$o = new old('Robert');
echo $o->name . "\n";

$f = new fresh('Hannes');
echo $f->greeting . "\n";

$foo = new faulty('Bitta');
echo $foo->greeting . "\n";

The result is:


Robert
Greetings Hannes
Hey Bitta