note 107495 added to function.get-class
| From: | Voitcus at wp dot pl | Date: | Mon, 13 Feb 2012 20:31:24 +0000 |
| Subject: | note 107495 added to function.get-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186107@lists.php.net to get a copy of this message | ||
Just to extend what Lanselot wrote on 11-Feb-2009
(http://php.mainseek.com/manual/en/function.get-class.php#88856).
There is a difference if you call this function with or without parameter in the class and one
inherited from it.
Let us consider (Lanselot's example slightly modified):
<?php
class A {
function foo() {
return get_class(); // no parameter
}
}
class B extends A {
}
$instance = new B();
echo $instance->foo(); //Prints 'A', not 'B' as might be expected.
?>
Instead try this:
<?php
class A {
function foo() {
return get_class($this); // the $this parameter!
}
}
class B extends A {
}
$instance = new B();
echo $instance->foo(); //Prints 'B'
?>
----
Server IP: 82.177.164.100
Probable Submitter: 89.75.186.180
----
Manual Page -- http://www.php.net/manual/en/function.get-class.php
Edit -- https://master.php.net/note/edit/107495
Del: integrated -- https://master.php.net/note/delete/107495/integrated
Del: useless -- https://master.php.net/note/delete/107495/useless
Del: bad code -- https://master.php.net/note/delete/107495/bad+code
Del: spam -- https://master.php.net/note/delete/107495/spam
Del: non-english -- https://master.php.net/note/delete/107495/non-english
Del: in docs -- https://master.php.net/note/delete/107495/in+docs
Del: other reasons-- https://master.php.net/note/delete/107495
Reject -- https://master.php.net/note/reject/107495
Search -- https://master.php.net/manage/user-notes.php