note 78136 deleted from language.oop.constructor by danbrown
| From: | danbrown@php.net | Date: | Sat, 07 Nov 2009 18:57:00 +0000 |
| Subject: | note 78136 deleted from language.oop.constructor by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162521@lists.php.net to get a copy of this message | ||
Note Submitter: mbrown at fensystems dot co dot uk
----
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....
}
}
?>