473,326 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

OOP: constructors of base class in new()

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
4 2254
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
3
by: Akeel Ahmed | last post by:
Hi I feel real silly asking this, I have been posed with a simple design question which goes as follows (I am from a procedural background and not oop which may help explain my stupidity) I...
11
by: Alberto Giménez | last post by:
Hi, I've seen some object oriented programming bits out there and i'm not sure if they're legal. For example: struct Object { int field1; int field2; }; struct SubObject { int field1; /*...
10
by: Kevin Buchan | last post by:
I searched the news group and could not find an answer to this question, so I'll go ahead and post it. Let's say I have a class A with a couple different constructors... nothin' special. Now,...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
1
by: John | last post by:
Hi, Maybe someone can help me with the following: "The first task by any derived class constructor is to call it’s direct or indirect base class constructor implicitly or explicitly", reads the...
4
by: st_ev_fe | last post by:
Hi people, I've been doing C for about 7 years now. But I'm new to C++. I've decided that C++'s operator overloading could be very handy. I'm writing something much like auto_ptr, except for...
5
by: Michael Thompson | last post by:
I am new to .Net and OOP techniques; I am not even certain that I using the correct terminology here. I believe that I want to know how to do inheritance. I want to create a custom "string"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.