Static methods in PHP

前端 未结 6 745
走了就别回头了
走了就别回头了 2020-12-15 04:38

Why in PHP you can access static method via instance of some class but not only via type name?

UPDATE: I\'m .net developer but i work with php developers too. Recent

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-15 05:08

    In PHP 7 it seems to be absolutely necessary for you to be able to do $this->staticFunction(). Because, if this code is written within an abstract class and staticFunction() is also abstract in your abstract class, $this-> and self:: deliver different results!

    When executing $this->staticFunction() from a (non-abstract) child of the abstract class, you end up in child::staticFunction(). All is well.

    However, executing self::staticFunction() from a (non-abstract) child of the abstract class, you end up in parent::staticFunction(), which is abstract, and thusly throws an exception.

    I guess this is just another example of badly designed PHP. Or myself needing more coffee...

提交回复
热议问题