04-25-2004, 02:09 PM
|
#1
|
|
Senior Member
Join Date: Aug 2002
Location: Philly
Posts: 2,583
|
PHP: Class extending a Class
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.
__________________
I need a new signature!
|
|
|
04-25-2004, 02:54 PM
|
#2
|
|
Senior Member
Join Date: Aug 2002
Location: Philly
Posts: 2,583
|
This is a bit strange....
If I want Class2 constructure called, I cannot have a constructor in Class3.
__________________
I need a new signature!
|
|
|
04-25-2004, 11:13 PM
|
#3
|
|
spend my time in blender
Join Date: Oct 2003
Location: tampa, fl
Posts: 1,138
|
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?
__________________

Certified Flash MX 2004 Developer
|
|
|
04-26-2004, 12:13 AM
|
#4
|
|
Senior Member
Join Date: Aug 2002
Location: Philly
Posts: 2,583
|
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.
__________________
I need a new signature!
|
|
|
04-26-2004, 03:00 AM
|
#5
|
|
thinking is design
Join Date: Apr 2003
Location: UK
Posts: 1,292
|
I think you have to call super () manually if you're using php 4, but not totally sure on that.
|
|
|
04-26-2004, 03:17 AM
|
#6
|
|
thinking is design
Join Date: Apr 2003
Location: UK
Posts: 1,292
|
Yeah, look at this:
PHP Code:
<?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 Code:
<?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;
?>
Last edited by retrotron; 04-26-2004 at 03:20 AM.
|
|
|
04-26-2004, 04:39 AM
|
#7
|
|
spend my time in blender
Join Date: Oct 2003
Location: tampa, fl
Posts: 1,138
|
Yeah, I am an idiot.  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
__________________

Certified Flash MX 2004 Developer
|
|
|
04-26-2004, 09:31 AM
|
#8
|
|
thinking is design
Join Date: Apr 2003
Location: UK
Posts: 1,292
|
Heh heh, I noticed the 'actionscript', and I just laughed. Hee hee, good times, good times.
|
|
|
04-26-2004, 09:34 AM
|
#9
|
|
spend my time in blender
Join Date: Oct 2003
Location: tampa, fl
Posts: 1,138
|
Well, as long as I provided you with a bit of entertainment, my public embarassment was worth every second.
__________________

Certified Flash MX 2004 Developer
|
|
|
04-26-2004, 09:39 AM
|
#10
|
|
thinking is design
Join Date: Apr 2003
Location: UK
Posts: 1,292
|
:d :d :d
<edit>
...um....those are supposed to be grins...
</edit>
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 12:23 AM.
///
|
|