note 53732 added to language.oop5
| From: | Obeliks at osu1 dot php dot net | Date: | Fri, 10 Jun 2005 21:56:35 +0000 |
| Subject: | note 53732 added to language.oop5 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90244@lists.php.net to get a copy of this message | ||
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)
----
Manual Page -- http://www.php.net/manual/en/language.oop5.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53732
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53732&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53732&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53732&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53732&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53732&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53732&report=yes
Search -- http://master.php.net/manage/user-notes.php