For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > March 2004 > OOP: constructors of base class in new()









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author OOP: constructors of base class in new()
Ramprasad A Padmanabhan

2004-03-26, 11:17 pm

Hi all,

I am a php newbie, ( doing perl all time till now ). I am a bit
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
Rampra
Karl Heinz Marbaise

2004-03-26, 11:17 pm

Hi Rampra,


[...]

> 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()

You have to write it explicitly into the Constructors of
the derived class to call the constructor of the parent class....
I know it is annoying but this is PHP... ;-)

Kind Regards.
Karl Heinz
--
Dipl.Ing.(FH) Karl Heinz Marbaise | www.marbaise.org
Jabba Dabba Dooh ;-) | ICQ# 135949029

Henk Verhoeven

2004-03-29, 6:43 pm

Hi Rampra,

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)

Rampra A Padmanabhan wrote:
> Hi all,
>
> I am a php newbie, ( doing perl all time till now ). I am a bit
> 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
> Rampra


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com