Simple question, what's $this - a or b?
<?php
class a {
function aa(){
var_dump($this);
}
}
class b {
function bb(){
a::aa();
}
}
$b_obj = new b;
$b_obj->bb();
?>
Answer: object(b)#1 (0) { }
This entry was posted
on Thursday, August 31st, 2006 and is filed under PHP certification.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
February 20th, 2007 at 7:02 am
Althoug code is very small, but thats sufficient to explain the meaning of $this. I loved it.
Thanks for a brainy posting.
October 18th, 2007 at 3:35 am
the answer is a , because $this that mean this is to this class
May 26th, 2008 at 11:36 am
$this always is a reference to the calling object
In your code as $b_obj object is calling bb() method of class b , so the output will be class b
July 22nd, 2008 at 3:45 am
ShaymoL is right. if we do the reverse i.e if we create object of class a and call b, like the code below,
aa();
?>
the answer will be a and not b.
December 23rd, 2008 at 3:17 pm
it will give this output
object(b)#1 (0) { }
but if you ask what $this-> it refer for a, cause it refer for it’s class
but i don’t know what this line mean
a::aa();
specially the “::” marks.