Calling Static Method from Class B(which extends Class A) of Class A

后端 未结 5 1647
遇见更好的自我
遇见更好的自我 2021-01-03 04:02

There was an interesting question in a practice test that I did not understand the answer to. What is the output of the following code:



        
5条回答
  •  萌比男神i
    2021-01-03 04:30

    $this to the object in whose context the method was called. So: $this is $a->getName() is $a. $this in $fooInstance->getName() would be $fooInstance. In the case that $this is set (in an object $a's method call) and we call a static method, $this remains assigned to $a.

    Seems like quite a lot of confusion could come out of using this feature. :)

提交回复
热议问题