Hi Ramprasad,
You can make code that does:
$currentClassName = get_class($this);
while (!empty($currentClassName = get_parent_class($currentClassName)) ) {
$this->$currentClassName();
}
However, this may not pass the proper parameters. Furthermore, if the
constructor of the parentclass does this too, higher constructors get
called twice. So i guess it will be better to code it recursively:
class Midleclass extends Superclass {
function Midleclass($param1) {
$parentClassName = get_parent_class($this);
$this->$parentClassName();
}
}
class Subclass extends Midleclass {
function Subclass($param1, $param2) {
$parentClassName = get_parent_class($this);
$this->$parentClassName($param1);
}
}
This has the advantage that if you rename a parent class (superclass),
you do not have to change the constuctor calls in its direct child
classes (subclasses). But is is more code then simply call the parent
constructor, and if you change the arguments of a parent constructor,
you may have chanche the calls anyhow.
Greetings,
Henk Verhoeven,
phpPeanuts.org
(Included code was not tested)
Ramprasad A Padmanabhan wrote:
Hi all,
I am a php newbie, ( doing perl all time till now ). I am a bit
confused OOP in php.
Suppose I create an instance of a class with the new() function the
cronstuctor is called .. fine
Now if the class *extends* another class the constructor of base class
is not called. But is there a way I can tell php to execute all
constructors of base classses when I call new()
Thanks
Ramprasad