parent::method() - calling non static method

百般思念 提交于 2019-12-04 07:49:32

If you will look at the definition of static method you will see:

  1. Static methods are meant to be relevant to all the instances of a class rather than to any specific instance. - indeed this method is relevant to all children of the parent class.
  2. A static method can be invoked even if no instances of the class exist yet. - again, you never create an instance of the parent class to invoke the method.

So we can take this argument as an excuse for PHP. By the way, in C++ it is done the same way.

But there are other languages, where it is done like you said. For example, in JAVA, the parent method called like super.printMethod();, in C#, it is done like base.printMethod().

So in PHP it might be done for the parser simplicity, as they will need a specific edge case for such invocation parent->printMethod().

That notification means that you can't call a non-statically defined method as static, but the call you did inside the method is not a static call, but a call to the parent class.

So this call will throw the E_STRICT warning:

$b = new B;
$b::example();

but your example will not

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!