Am 15.02.2015 um 19:09 schrieb Rowan Collins:
<snip>

The static modifier for methods is part of the method signature and
method body isn't.
(That's way interfaces doesn't describe method bodies but signatures)

The static modifier defines a method as static and therefore defines
the
method is callable using "::".
More specifically, it defines that the method is *only* callable statically. 
There is currently no opposite keyword to say that a method can only be called 
non-statically, but the absence of a modifier has up until now meant that 
either call type is possible. An interface cannot currently forbid static 
calls, only the use of $this.

Our choice is between keeping that ambiguity, and introducing a new rule that 
any method not marked static must never be called statically (with provisos for 
technically non-static parent:: etc).

Regards,

The problem with the current behavior of "... the absence of a modifier has up until now meant that either call type is possible" is that the library author can't define a method as non static callable and the caller defines if $this can be used.

As a simple example (again). A library defines a class like that:

class A {
    protected $base = 1;
    public function doSomething() {
        return $this->base * 2;
    }
    public function doSomethingMore() {
        return (2 * 2) + 4;
    }
}

Now you notes a bug in "doSomethingMore()" as it calculates a wrong number and you go to fix that into the following:

class A {
    protected $base = 1;
    public function doSomething() {
        return $this->base * 2;
    }
    public function doSomethingMore() {
        return $this->doSomething() + 4;
    }
}

You have done a simple bugfix but this kind of change generates a big bc brea if someone calls A::doSomethingMore().

The message of the current behavior is "Hey you calls the function wrongly. This works but it's not safe." but the message isn't consistent. The change of Nikita makes the massage consistent and that is a very good step forward. Additionally Nikita is going to change the message a little into "Hey you calls the function wrongly. This works but it's not safe AND WILL BE REMOVED IN THE FUTURE". It's something I like because the issue described above will be catched earlier and the caller have to make it right from the beginning.

Removing the message would mean the library author has the fault here because it would mean the caller does it right and it's allowed and common in PHP to call a method statically even if it wasn't defined as it. The library author would end up in something hackery to use the variable $this without any need only to define a method as non static.

PS: A static call of parent::doSomething() and self::doSomething() from an instance context is something special because it's widely used in PHP and changing this would be a big bc break. (Personally I would like to see something like parent->doSomething() and self->doSomething() would work, too)

Thanks
Marc


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to