note 53732 deleted from language.oop5 by felipe
| From: | felipe@php.net | Date: | Mon, 24 Mar 2008 22:11:25 +0000 |
| Subject: | note 53732 deleted from language.oop5 by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137471@lists.php.net to get a copy of this message | ||
Note Submitter: Obeliks
----
You can call parent::__construct(), even if the class yours inherits from uses the old constructor
style "Classname()".
Example:
<?php
class A {
function A() {
echo 'Constructor of A<br/>';
}
}
class B extends A {
function __construct() {
parent::__construct();
echo 'Constructor of B<br/>';
}
}
class C extends A {
function __construct() {
parent::A();
echo 'Constructor of C<br/>';
}
}
$b = new B();
echo '<br/>';
$c = new C();
/* will output:
Constructor of A
Constructor of B
Constructor of A
Constructor of C
*/
?>
So you see you can also call parent::Classname() if your superclass uses this format. Keep in mind
that it doesn't work the other way round though:
<?php
class A {
function __construct() {
echo 'Constructor of A<br/>';
}
}
class B extends A {
function __construct() {
parent::A();
echo 'Constructor of B<br/>';
}
}
$b = new B();
/* will output:
Fatal error: Call to undefined method A::a() in __FILE__ on line __LINE__
*/
?>
So it's always the best choice to use the __construct style! (at least if you're running
PHP5)