On Sun, Oct 12, 2014 at 10:37 AM, Robert Stoll <p...@tutteli.ch> wrote:

> Hey,
>
>
>
> I just stumbled over a method call of a non-static method with self and
> was asking myself again, why does PHP support
> this behaviour.  An example to outline what I am writing of:
>
>
>
> class A{
>
>   function foo(){
>
>     self::bar();
>
>   }
>
>   function bar(){}
>
> }
>
>
>
> IMO it should not be allowed to call non-static methods with self or
> static. Sure, it is just a small detail but for
> someone who starts learning PHP it is an unnecessary supplement.
>
> Maybe it is too drastic to disallow it in PHP 7 but yeah. I would like to
> know what you think about it and if someone
> has a good reason why it is allowed nowadays then please help me out.
>

There's a common misconception that ::foo() denotes a static method call in
PHP. What it actually does is a *scoped* call (which is why :: is called
the "scope resolution operator" and not the "static access operator").

What :: essentially does is give you the ability to call the implementation
of a method in a particular class. A common application is the use of
parent::foo() which will not call your implementation of foo(), but the one
found in the parent class. Similarly you can use A::foo() to target a
particular class that is even further up in the inheritance hierarchy
(like, the grandparent-class). You can also call call a class that is
completely outside your inheritance hierarchy, but that's deprecated since
PHP 5.6 and will hopefully be removed in PHP 7.

Nikita

Reply via email to