2
interface PasswordBrokerContract
{
    const PASSWORD_RESET = 'passwords.reset';

    public function reset(array $credentials, Closure $callback);
}

class PasswordBroker implements PasswordBrokerContract
{
    public function reset(array $credentials, Closure $callback)
    {
        // implementation

        return static::PASSWORD_RESET;
    }

}

Does it matter if you use self or static considering that interface constants can't be overridden by a class/interface that inherits them?

yivi
  • 42,438
  • 18
  • 116
  • 138
AlexBor
  • 151
  • 2
  • 12
  • I guess, you can not override interface constant (but you can override class constant). After overriding you would get fatal error ```Cannot inherit previously-inherited or override constant ``` – Andrii Filenko Nov 27 '19 at 09:48
  • @yivi In a way that it will always be the same result no matter if you use `self` or `static`. – AlexBor Nov 27 '19 at 09:55

1 Answers1

2

It could matter, because even if you can't override interface constants in classes implementing an interface, you can override these constants in classes extending the class that originally implemented the interface:

E.g. the following:

<?php

interface Contract
{
    const PASSWORD_RESET = 'passwords.reset';
    public function reset(): void;
}


class A implements Contract
{
    public function reset(): void
    {
        echo static::PASSWORD_RESET, "\n";
    }
}

class B extends A {
    const PASSWORD_RESET = 'foobar';
}

Now this:

(new A())->reset();

would output passwords.reset.

But this:

(new B())->reset();

would output foobar.

But if you didn't use late static binding, and used self, you'd get passwords.reset every time.

See it working here.

yivi
  • 42,438
  • 18
  • 116
  • 138