note 78136 added to language.oop.constructor
| From: | mbrown at fensystems dot co dot uk | Date: | Sat, 29 Sep 2007 16:44:17 +0000 |
| Subject: | note 78136 added to language.oop.constructor | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130927@lists.php.net to get a copy of this message | ||
I found a reasonably neat way in PHP4 to solve the problem of calling superclass constructors
without needing to embed knowledge of the entire class hierarchy into each class:
<?php
class A {
function A () { $this->constructor(); }
function constructor () {
....this is the "A" constructor body....
}
}
class B extends A {
function B () { $this->constructor(); }
function constructor() {
/* Call superclass constructor */
parent::constructor();
....this is the "B" constructor body....
}
}
?>
Note that the fact that class B inherits from class A is encoded in precisely one place: the
"extends" clause, and is not duplicated within the class B constructor body, as would be
the case had we used something more traditional:
<?php
class B extends A {
function B() {
$this->A(); /* "extends A" information would be duplicated here */
....this is the "B" constructor body....
}
}
?>
----
Server IP: 87.124.64.96
Probable Submitter: 81.107.32.114
----
Manual Page -- http://www.php.net/manual/en/language.oop.constructor.php
Edit -- https://master.php.net/note/edit/78136
Del: integrated -- https://master.php.net/note/delete/78136/integrated
Del: useless -- https://master.php.net/note/delete/78136/useless
Del: bad code -- https://master.php.net/note/delete/78136/bad+code
Del: spam -- https://master.php.net/note/delete/78136/spam
Del: non-english -- https://master.php.net/note/delete/78136/non-english
Del: in docs -- https://master.php.net/note/delete/78136/in+docs
Del: other reasons-- https://master.php.net/note/delete/78136
Reject -- https://master.php.net/note/reject/78136
Search -- https://master.php.net/manage/user-notes.php