在PHP中的扩展类中重新定义方法时返回正确的类名

I expected the first example code produces the result same as the second one but it didn't. So in order to get the correct class name, I have to redefine the custom msg() method in the extended class.

I'd like to know why the first one does not produce the extended class name and why it does when the method is redefined which is exactly the same.

Example 1

call_user_func(array(new MyClass_Mod, "msg"));

class MyClass {
    function msg() {
        echo '<p>get_class(): ' . get_class() . '</p>';
    }
}
class MyClass_Mod extends MyClass {
}

output

get_class(): MyClass

Example 2

call_user_func(array(new MyClass_Mod, "msg"));

class MyClass {
    function msg() {
        echo '<p>get_class(): ' . get_class() . '</p>';
    }
}
class MyClass_Mod extends MyClass {
    function msg() {
        echo '<p>get_class(): ' . get_class() . '</p>';
    }
}

output

get_class(): MyClass_Mod

I'd like to know the mechanism, so please do not suggest to use get_called_class(). I'm not able to use it for the version below PHP 5.3. Thanks for your input.

You just have to pass $this argument to get_class function.

get_class($this);

If the parameter of get_class is omitted when inside a class, the name of that class is returned.

You need to specify the parameter with $this.