473,581 Members | 2,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class constructor inheritance

I found an odd situation that probably isn't doing what is intended.

class A {
function A() {
// normal constructor for class1
}

function B() {
// a general purpose method
}
}

class B extends A {
// this class will inherit A::B as a constructor
}
Jul 17 '05 #1
11 4664
tc*****@savageR esearch.com (Tim Cadell) wrote in message news:<db******* *************** ****@posting.go ogle.com>...
I found an odd situation that probably isn't doing what is intended.

class A {
function A() {
// normal constructor for class1
}

function B() {
// a general purpose method
}
}

class B extends A {
// this class will inherit A::B as a constructor
}


I'm not sure I understand what you are saying. If I understood you,
you are saying class B picks up class A's constructor. Why would that
be odd? That is how it works in Java, and, I would guess, most OOP
languages. The constructors each execute in the order of the class
hierarchy, starting at the top and working down to the subclass.

Or did I misunderstand you?
Jul 17 '05 #2
Hi Tim,
I found an odd situation that probably isn't doing what is intended.

class A {
function A() {
// normal constructor for class1
}

function B() {
// a general purpose method
}
}

class B extends A {
// this class will inherit A::B as a constructor
}

The inheritance of the construct will result, cause
your class B has not defined a Constructor.
If you like to change this you have to override the
inherited construcotr with your own version....
That like in C++ ? What is odd on that?

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

Jul 17 '05 #3
Karl Heinz Marbaise <kh********@gmx .de> wrote in message news:<c4******* ******@ID-68093.news.uni-berlin.de>...
Hi Tim,
I found an odd situation that probably isn't doing what is intended.

class A {
function A() {
// normal constructor for class1
}

function B() {
// a general purpose method
}
}

class B extends A {
// this class will inherit A::B as a constructor
}

The inheritance of the construct will result, cause
your class B has not defined a Constructor.
If you like to change this you have to override the
inherited construcotr with your own version....
That like in C++ ? What is odd on that?

But even if he has a constructor in class B, the constructor in class
A still executes as part of the construction of class B, yes?
Jul 17 '05 #4
> But even if he has a constructor in class B, the constructor in class
A still executes as part of the construction of class B, yes?


None of this is what he is observing.

First of all, the answer to the above question is no. If class B has a
constructor, and extends class A (which also has a constructor), then class
A's constructor *will* *not* be called when you create a B object, unless
you either

1) There is no constructor for class B
2) You explicitly call the constructor for A in the constructor for B

Now, what Tim was saying is this:

class Shape {
function Shape () {
echo "I am a shape!";
}

function Square() {
echo "I have sharp corners!";
}
}

class Square extends Shape{
// No constructor defined
}

Now, the class Square does not have constructor, so it inherits the parent
constructor, right? Wrong. It has a constructor, Square::Square( ). Doing

$S = new Square();

would result in "I have sharp corners!" being output, but not "I am a
shape!"

NB: I have not actually tested this, I am just explaining what Tim was
saying. And it makes sense, based on what I know of PHP internals, that it
would be this way. In a hard core language, like C++, I doubt it would
behave like this. That is what he is observing.
Jul 17 '05 #5
"Joshua Beall" <jb****@donotsp am.remove.me.he raldic.us> wrote in message
Now, what Tim was saying is this:

class Shape {
function Shape () {
echo "I am a shape!";
}

function Square() {
echo "I have sharp corners!";
}
}

class Square extends Shape{
// No constructor defined
}

Now, the class Square does not have constructor, so it inherits the parent
constructor, right? Wrong. It has a constructor, Square::Square( ). Doing

$S = new Square();

would result in "I have sharp corners!" being output, but not "I am a
shape!"

That's the kind of bug that would leave me dazed for days. Like, the
other day I wrote two class methods with the same name, but different
signatures, and I assumed PHP would tell the difference based on the
signature. Turns out I was wrong, but it took me awhile to realize
what the problem was.
Jul 17 '05 #6
"Joshua Beall" <jb****@donotsp am.remove.me.he raldic.us> wrote in message
news:Ut******** ******@nwrddc03 .gnilink.net...
NB: I have not actually tested this, I am just explaining what Tim was
saying. And it makes sense, based on what I know of PHP internals, that it would be this way. In a hard core language, like C++, I doubt it would
behave like this. That is what he is observing.


I reproduced this behavior on PHP 4.3.4. Pretty weird. I know this was a
problem in PHP 3 but that was supposed to have been fixed a long time ago.
According to the manual, "A constructor is a function of the same name as
the class it is being defined in."

Jul 17 '05 #7
> That's the kind of bug that would leave me dazed for days. Like, the
other day I wrote two class methods with the same name, but different
signatures, and I assumed PHP would tell the difference based on the
signature. Turns out I was wrong, but it took me awhile to realize
what the problem was.


I thought PHP would give an error about redefining a function. What does it
actually do? Just use the first one?
Jul 17 '05 #8
"Joshua Beall" <jb****@donotsp am.remove.me.he raldic.us> wrote in message news:<EA******* *********@nwrdd c03.gnilink.net >...
That's the kind of bug that would leave me dazed for days. Like, the
other day I wrote two class methods with the same name, but different
signatures, and I assumed PHP would tell the difference based on the
signature. Turns out I was wrong, but it took me awhile to realize
what the problem was.


I thought PHP would give an error about redefining a function. What does it
actually do? Just use the first one?


It just used the last one, which wasn't the one I wanted.
Jul 17 '05 #9
> It just used the last one, which wasn't the one I wanted.

I think the only way to do this would be to check to see what variables were
passed (NB: if they get passed NULL, it will be the same as if they were not
passed at all - using isset() won't help :-/ ), then either do and if/else,
if/elseif/else, or switch() setup.
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
6321
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the...
7
13203
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
8
1570
by: Nick | last post by:
I have the following code: var obj = {a:0, b:1, ...} function f() {...} obj.f = f // This will make a instance method? How to make a Class method for the class of obj? Or what's the class of "obj"?
9
4982
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived...
3
1586
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client app with; D m_D = new D(tkn); public class A : MarshalByRefObject public A () <--------------------- public class B : A public B ()...
4
2652
by: Francisco Amaro | last post by:
Hi all, Have question about inheriting a class that has parameters in the constructor such as : Public MustInherit Class MyParentClass Public mystring As String Public Sub New(ByVal paramstring As String)
3
2234
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
5
2654
by: petethebloke | last post by:
I'm not sure how to ask for what I want, so I can't find it on Google. I'll explain..... I have a class 'hanging' with a constructor: class hanging{ function hanging(){ }
11
18323
by: Rahul | last post by:
Hi Everyone, While working with Java, i came across super() which passes values to base class constructor from derived class constructor. I was wondering if this could be implemented in c++ by any mechanism as super is not supported by c++, atleast by MS vc++ 6.0.
6
3199
by: Xu, Qian | last post by:
Hello All, is there any handy tool to generate class diagrams for javascript? I have tried JS/UML, but it generates always an empty diagram. -- Xu, Qian (stanleyxu) http://stanleyxu2005.blogspot.com
0
7860
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8139
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8299
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8166
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5667
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1398
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.