constructors in derived classes
| From: | Erik Price | Date: | Thu, 11 Apr 2002 18:04:44 +0000 |
| Subject: | constructors in derived classes | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-92626@lists.php.net to get a copy of this message | ||
I have another question about OO in PHP --
The manual (http://www.php.net/manual/en/language.oop.constructor.php) does an excellent job of explaining how constructors in extended classes work when there are constructors in base classes. I understand that no problem. But at the bottom of the page is a cautionary note:
<cautionary_note>
Neither PHP 3 nor PHP 4 call constructors of the base class automatically from a constructor of a derived class. It is your responsibility to propagate the call to constructors upstream where appropriate.
</cautionary_note>
This is where I am uncertain -- how do I call an upstream constructor from an instance of an extended class, where I have a constructor in the extended class? IOW:
<?php
class ClassA
{
function ClassA($var)
{
print "ClassA object instantiated, argument is $var";
}
}
class ClassB
{
function ClassB($var)
{
print "ClassB object instantiated, argument is $var";
}
}
$instance = new ClassB(frank);
// this should print "ClassB object instantiated, argument is frank"
My question is, how do I call the constructor of ClassA? To see the following results:
// "ClassB object instantiated, argument is frank"
// "ClassA object instantiated, argument is bob"
Erik
----
Erik Price
Web Developer Temp
Media Lab, H.H. Brown
pricee@hhbrown.com