Re: disallow non-static method calls with self/static in PHP 7
| From: | Tjerk Meesters | Date: | Sun, 12 Oct 2014 09:18:06 +0000 |
| Subject: | Re: disallow non-static method calls with self/static in PHP 7 | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-77897@lists.php.net to get a copy of this message | ||
On 12 Oct 2014, at 16:37, Robert Stoll <php@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.
The use of self::, static:: (or parent:: for that matter) itself doesn’t make a method call
static; it’s the declaration or caller context that makes it behave statically, i.e. $this can’t
be used.
In your given example, $this is defined when A::bar() is called; interestingly, when this class is
extended it will still only call A::bar() as opposed to calling $this->bar(). Whether this is
useful behaviour is irrelevant :)
The use of static::bar() is kind of pointless, because AFAICT there’s no difference between that
and $this->bar() in this particular example.
>
> 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.
>
>
>
> Cheers,
>
> Robert
>