note 39558 added to function.get-class
| From: | MagicalTuxatFF dot ST at rn2 dot php dot net | Date: | Mon, 02 Feb 2004 09:11:31 +0000 |
| Subject: | note 39558 added to function.get-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64545@lists.php.net to get a copy of this message | ||
Note that the constant __CLASS__ is different from get_class($this) :
<?
class test {
function whoami() {
echo "Hello, I'm whoami 1 !\r\n";
echo "Value of __CLASS__ : ".__CLASS__."\r\n";
echo "Value of get_class() : ".get_class($this)."\r\n\r\n";
}
}
class test2 extends test {
function whoami2() {
echo "Hello, I'm whoami 2 !\r\n";
echo "Value of __CLASS__ : ".__CLASS__."\r\n";
echo "Value of get_class() : ".get_class($this)."\r\n\r\n";
parent::whoami(); // call parent whoami() function
}
}
$test=new test;
$test->whoami();
$test2=new test2;
$test2->whoami();
$test2->whoami2();
?>
The output is :
Hello, I'm whoami 1 !
Value of __CLASS__ : test
Value of get_class() : test
Hello, I'm whoami 1 !
Value of __CLASS__ : test
Value of get_class() : test2
Hello, I'm whoami 2 !
Value of __CLASS__ : test2
Value of get_class() : test2
Hello, I'm whoami 1 !
Value of __CLASS__ : test
Value of get_class() : test2
In fact, __CLASS__ returns the name of the class the function is in and get_class($this) returns the
name of the class which was created.
----
Manual Page -- http://www.php.net/manual/en/function.get-class.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39558
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39558&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39558&report=yes
Search -- http://master.php.net/manage/user-notes.php