note 57780 added to function.get-class
| From: | taylorbarstowatthatgooglemailthingy at osu1 dot php dot net | Date: | Thu, 13 Oct 2005 22:41:38 +0000 |
| Subject: | note 57780 added to function.get-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-96986@lists.php.net to get a copy of this message | ||
Interesting behavior in PHP5; if you don't pass $this from within an object method, it returns
the name of the current class; if you do pass $this, it returns the name of the class of the object
(which might not be the current class due to inheritance).
<?php
class MyParent {
function whoami() {
echo 'Without passing $this, we have: ' . get_class() . "\n";
echo 'Passing $this, we have: ' . get_class($this) . "\n";
}
}
class MyChild extends MyParent { }
$c = new MyChild;
$c->whoami();
?>
Output:
Without passing $this, we have: MyParent
Passing $this, we have: MyChild
So, get_class() without $this is like __CLASS__.
----
Manual Page -- http://www.php.net/manual/en/function.get-class.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57780
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57780&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57780&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57780&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57780&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57780&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57780&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57780&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57780&report=yes
Search -- http://master.php.net/manage/user-notes.php