By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,643 Members | 1,797 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,643 IT Pros & Developers. It's quick & easy.

OOP: constructors of base class in new()

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Ramprasad A Padmanabhan" <ra**************************@oracle.com>
wrote in message news:xn*************@news.oracle.com...
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


You must call them explicitly. E.g.:
$this->BaseClass();
Goran
Jul 17 '05 #2

P: n/a
Hi Ramprasad,
[...]
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

Jul 17 '05 #3

P: n/a
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()


I can't see why would you want to do that. If the classes are so different
that each subclass needs a new contructor, then you don'te need inheritance,
but separate classes. If, on the other hand, you need the subclass to call
the parent's contructor, instead of its own, then don't give the subclass a
constructor. E.g:

class DarthVader {
function DarthVader() {} // constructor
}

class LukeSkywalker extends DarthVader {
// no constructor
}

new LukeSkywalker() will then call the DarthVader() function.

Berislav

--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.
Jul 17 '05 #4

P: n/a
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


Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.