note 35789 added to keyword.parent
| From: | adrian at rn2 dot php dot net | Date: | Sun, 14 Sep 2003 23:48:24 +0000 |
| Subject: | note 35789 added to keyword.parent | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-56487@lists.php.net to get a copy of this message | ||
if you like to use the here suggested method of calling a parent constructer by using
get_parent_class(), you will expirience undesired behaviour, if your parent is using the same
technique.
class A {
function A() {
$me = get_class($this);
echo "A:this is $me, I have no parent\n";
}
}
class B extends A {
function B() {
$par = get_parent_class($this);
$me = get_class($this);
echo "B:this is $me, my parent is $par\n";
parent::$par();
}
}
class C extends B {
function C() {
$par = get_parent_class($this);
$me = get_class($this);
echo "C:this is $me, my parent is $par\n";
parent::$par();
}
}
new C();
this will not produce the output you probably expected:
C:this is c, my parent is b
B:this is b, my parent is a
A:this is a, i have no parent
...but insted you will get in serious troubles:
C:this is c, my parent is b
B:this is c, my parent is b
Fatal error: Call to undefined function: b() in .............. on line 16
$this is always pointing to the 'topmost' class - it seems like this is php's way to
cope with polymorphic OOP.
----
Manual Page -- http://www.php.net/manual/en/keyword.parent.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35789
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35789&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35789&report=yes