PDA

View Full Version : PHP: Class extending a Class


boyzdynasty
04-25-2004, 02:09 PM
Class1

Class2 extends Class1

Class3 extends Class2


In Class2 I have a constructor that does not take in any arguements but it sets up some value to some variables.

When create an object of Class3, I assumed that the constructor for Class2 would be called and set up values for the variables in Class2.

But it didn't.

$test = new Class3();

echo "some value: $test->var1"; // var1 is a variable from Class2

The result was empty (nothing was set).

How do I get the Constructor of Class2 to be executed when I create an object of Class3 ?


Thanks in advance.

boyzdynasty
04-25-2004, 02:54 PM
This is a bit strange....

If I want Class2 constructure called, I cannot have a constructor in Class3.

splict
04-25-2004, 11:13 PM
Well, in order to call the super's constructors or methods you should use super(); or super.myMethod(); However, if you don't explicitly call the super's constructor, I think actionscript should call it automatically. Could you post a bit of code, or a short example?

boyzdynasty
04-26-2004, 12:13 AM
i was thinking that too...cuz that is how i do it in JAVA

but I did a search for php and super...and didn't find anything.

So i figure i can't do that.

retrotron
04-26-2004, 03:00 AM
I think you have to call super () manually if you're using php 4, but not totally sure on that.

retrotron
04-26-2004, 03:17 AM
Yeah, look at this:

<?php

class Class1 {

function Class1 () {
print ('Class1 constructor invoked <br />');
} // end constructor

function Class1_foo () {
print ('Class1_foo () invoked <br />');
} // end Class1_foo ()

} // end Class1

class Class2 extends Class1 {

function Class2 () {
print ('Class2 constructor invoked <br />');
} // end constructor

} // end Class2

$c = new Class2;

?>


If you run that, only the constructor for Class2 is invoked. So PHP doesn't automatically invoke superclass constructors. To do that, you have to manually call the superclass constructor with parent::superclassName (), like this:


<?php

class Class1 {

function Class1 () {
print ('Class1 constructor invoked <br />');
} // end constructor

function Class1_foo () {
print ('Class1_foo () invoked <br />');
} // end Class1_foo ()

} // end Class1

class Class2 extends Class1 {

function Class2 () {
parent::Class1 ();
print ('Class2 constructor invoked <br />');
} // end constructor

} // end Class2

$c = new Class2;

?>

splict
04-26-2004, 04:39 AM
Yeah, I am an idiot. :o Of course php doesn't automatically call the super's constructor. If you read my post carefully, you can see I wrote actionscript even though it was obvious you were using php. I guess if I'm not gonna take the proper amount of time to sit and read a post before I answer it, I probably shoudln't answer, huh? ;)

-splict

retrotron
04-26-2004, 09:31 AM
Heh heh, I noticed the 'actionscript', and I just laughed. Hee hee, good times, good times. ;)

splict
04-26-2004, 09:34 AM
Well, as long as I provided you with a bit of entertainment, my public embarassment was worth every second. ;) :D

retrotron
04-26-2004, 09:39 AM
:d :d :d

<edit>

...um....those are supposed to be grins...

:D :D :D

</edit>

boyzdynasty
04-26-2004, 09:42 AM
*lol

I guess I didn't read your post carefully, too. :D

boyzdynasty
04-26-2004, 09:44 AM
thanks retrotron.