note 52962 added to language.oop.constructor
| From: | jfonseca at matarese dot com | Date: | Thu, 19 May 2005 02:07:32 +0000 |
| Subject: | note 52962 added to language.oop.constructor | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89611@lists.php.net to get a copy of this message | ||
This will save you time if you're building a class hierarchy with 2 or more levels of
inheritance.
$this always refers to the CURRENT INSTANCE. Therefore if you have:
class A{
function A(){
}
// ....
}
class B extends A{
function B(){
// keep these 2 lines in mind
$parentClass = get_parent_class($this);
$this->$parentClass();
}
// ....
}
class C extends B{
function C(){
$parentClass = get_parent_class($this);
$this->$parentClass();
// here you will get into an infinite loop calling B();
}
// ....
}
Explanation: $this is an instance variable referring to the CURRENT INSTANCE, therefore the $this
variable on class B will always be an instance of C, therefore get_parent_class($this) in class B
will call the constructor of C's parent class, which is B again.
This is a hard to track error if you're a novice on PHP's OO features mainly because PHP
will not produce any warnings, errors or notices, nor will it log anything to Apache or the
error_log file.
Keywords : infinite recursion, parent constructor, subclass
----
Manual Page -- http://www.php.net/manual/en/language.oop.constructor.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52962
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52962&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52962&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52962&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52962&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52962&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52962&report=yes
Search -- http://master.php.net/manage/user-notes.php