note 129364 added to language.constants.magic
| From: | loren@maxwellemail.com | Date: | Sat, 30 Mar 2024 11:37:49 +0000 |
| Subject: | note 129364 added to language.constants.magic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221264@lists.php.net to get a copy of this message | ||
As Chris pointed out:
"__CLASS__ and __METHOD__ both reference the class the code is written in, not whatever the
object class is."
To get the class of a child object calling a method defined in the parent class use
get_class($this).
<?php
class bar {
public function __construct()
{
var_dump(__CLASS__);
var_dump(get_class());
var_dump(get_class($this));
}
}
class foo extends bar {
}
new foo;
?>
Output:
string(3) "bar"
string(3) "bar"
string(3) "foo"
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.131 (proxied: 73.6.56.230)
----
Manual Page -- https://php.net/manual/en/language.constants.magic.php
Edit -- https://main.php.net/note/edit/129364
Del: integrated -- https://main.php.net/note/delete/129364/integrated
Del: useless -- https://main.php.net/note/delete/129364/useless
Del: bad code -- https://main.php.net/note/delete/129364/bad+code
Del: spam -- https://main.php.net/note/delete/129364/spam
Del: non-english -- https://main.php.net/note/delete/129364/non-english
Del: in docs -- https://main.php.net/note/delete/129364/in+docs
Del: other reasons-- https://main.php.net/note/delete/129364
Reject -- https://main.php.net/note/reject/129364
Search -- https://main.php.net/manage/user-notes.php