note 25359 added to keyword.parent
| From: | mwwaygoo at hotmail dot com | Date: | Fri, 20 Sep 2002 13:54:29 +0000 |
| Subject: | note 25359 added to keyword.parent | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-37043@lists.php.net to get a copy of this message | ||
With the example given previous from johnremovethreewordsplatte@pobox.com
You should only do this if your initialisation is different (no point in extending if it isn't)
When you extend a class its constructor still remains valid and is executed when constructed.
Writing a new constructor in the extended class overwrites the old one - inheritance.
so the following will also work
class Superclass {
function Superclass($arg) {
$this->initialize($arg);
}
function initialize($arg) {
$this->setArg($arg);
}
}
class Subclass extends Superclass {
//initialize() is inherited:
//BUT so is constructor
function extra_stuff($arg) {
echo "extra stuff " . $arg;
}
}
THE constructor is the function who's name is the same as the class that it is defined within.
AND remains the constructor even in extended classes, until redefined.
Think of the constructor as a seperate function from all the others. As if PHP looks at your class
and makes a copy of the function of the same name as the class and calls it "Constructor".
You can also create a function within the extended class with the same name as the constructor in
the parent class without re-writing the constructor. Because you are not altering the
"Constructor" function.
(BUT any redefined variables are used in the constructor, as expected)
ie
class One
{
var $v = "one";
function One()
{
print "Class One::Constructor value=" . $this->v . "<br />\n";
}
}
class Two extends One
{
var $v = "two";
function one()
{
print "REDEFINED : Class Two::Function One value=" . $this->v . "<br
/>\n";
}
}
$o = new One();
$o->one();
$p = new Two();
$p->one();
results in :-
Class One::Constructor value=one
Class One::Constructor value=one
Class One::Constructor value=two
REDEFINED : Class Two::Function One value=two
--
http://www.php.net/manual/en/keyword.parent.php
http://master.php.net/manage/user-notes.php?action=edit+25359
http://master.php.net/manage/user-notes.php?action=delete+25359
http://master.php.net/manage/user-notes.php?action=reject+25359