dstk51319 2012-10-11 08:15
浏览 22
已采纳

在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.

  • 写回答

2条回答 默认 最新

  • dongyongyu0789 2012-10-11 08:16
    关注

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

    get_class($this);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制