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:
When you call $a->getName()
you're referencing a specific object, $a
, which is of class Bar
and so returns "John".
Foo::getName()
isn't valid outside the function because there's no specific object.
I'm not sure it works in PHP, but if you cast the object to the superclass as in (Foo)$a->getName()
then you'd get "Andrew" as your result. You'd still be talking about the specific object ($a
) but in this case of type Foo
. (Note you wouldn't generally want to do this)