note 24308 added to keyword.parent
| From: | johnremovethreewordsplatte at pobox dot com | Date: | Mon, 12 Aug 2002 21:16:12 +0000 |
| Subject: | note 24308 added to keyword.parent | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-34897@lists.php.net to get a copy of this message | ||
Combining the above suggestions with the "Pull Up Constructor Body" refactoring from
Fowler (http://www.refactoring.com/catalog/pullUpConstructorBody.html), I found an elegant way to
inherit constructors in PHP. Both superclass and subclass constructors call an initialize() method,
which can be inherited or overridden. For simple cases (which almost all of mine are), no calls to
parent::anything are necessary using this technique. The resulting code is easy to read and
understand:
class Superclass {
function Superclass($arg) {
$this->initialize($arg);
}
function initialize($arg) {
$this->setArg($arg);
}
}
class Subclass extends Superclass {
function Subclass($arg) {
//initialize() is inherited:
$this->initialize($arg);
}
}
--
http://www.php.net/manual/en/keyword.parent.php
http://master.php.net/manage/user-notes.php?action=edit+24308
http://master.php.net/manage/user-notes.php?action=delete+24308
http://master.php.net/manage/user-notes.php?action=reject+24308