dporu02280 2013-08-23 10:47
浏览 91
已采纳

为什么可以通过静态调用访问公共函数 - PHP? [重复]

This question already has an answer here:

Please review below code. Though I have not define test method as static It can be access by A::test(). How it is possible? Is it bug in PHP?

class A {       
        public function test(){         
            echo __CLASS__;
        }
}
A::test(); //Print 'A'

If I try to access any public member inside test() method, It is giving me an error. See below.

class A {   
        public $name = 'jimit';
        public static function test(){          
                        echo $this->name;
            echo __CLASS__;
        }
    }
A::test(); //Error

Please help me out to clarify the things.

Thanks, Jimit

</div>
  • 写回答

1条回答 默认 最新

  • dougang1605 2013-08-23 10:53
    关注

    It is not a bug, but rather a leftover from PHP4's object model implementation. In current versions calling nonstatic method statically will raise an E_STRICT level warning.

    As for the second one it is obvious, there's no instance of your class, so there is no $this to refer to.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播