472,989 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 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 2240
"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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.