note 71064 added to language.constants.predefined
| From: | TomekPerlak | Date: | Fri, 10 Nov 2006 10:16:18 +0000 |
| Subject: | note 71064 added to language.constants.predefined | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119580@lists.php.net to get a copy of this message | ||
The __CLASS__ magic constant nicely complements the get_class() function.
Sometimes you need to know both:
- name of the inherited class
- name of the class actually executed
Here's an example that shows the possible solution:
<?php
class base_class
{
function say_a()
{
echo "'a' - said the " . __CLASS__ . "<br/>";
}
function say_b()
{
echo "'b' - said the " . get_class($this) . "<br/>";
}
}
class derived_class extends base_class
{
function say_a()
{
parent::say_a();
echo "'a' - said the " . __CLASS__ . "<br/>";
}
function say_b()
{
parent::say_b();
echo "'b' - said the " . get_class($this) . "<br/>";
}
}
$obj_b = new derived_class();
$obj_b->say_a();
echo "<br/>";
$obj_b->say_b();
?>
The output should look roughly like this:
'a' - said the base_class
'a' - said the derived_class
'b' - said the derived_class
'b' - said the derived_class
----
Server IP: 66.163.161.117
Probable Submitter: 217.30.156.35
----
Manual Page -- http://www.php.net/manual/en/language.constants.predefined.php
Edit -- https://master.php.net/note/edit/71064
Del: integrated -- https://master.php.net/note/delete/71064/integrated
Del: useless -- https://master.php.net/note/delete/71064/useless
Del: bad code -- https://master.php.net/note/delete/71064/bad+code
Del: spam -- https://master.php.net/note/delete/71064/spam
Del: non-english -- https://master.php.net/note/delete/71064/non-english
Del: in docs -- https://master.php.net/note/delete/71064/in+docs
Del: other reasons-- https://master.php.net/note/delete/71064
Reject -- https://master.php.net/note/reject/71064
Search -- https://master.php.net/manage/user-notes.php