Skip to content

Conversation

El-Virus
Copy link

PHP doesn't support abstract constants, so using self-referencing constants in traits in order to force implementer classes to define the constant is not unheard of.
An example of such an implementation would be:

trait Greeter {
    private const string WHO = self::WHO;

    public static function greeting() : string {
        return "Hello, " . static::WHO;
    }
}

class JackGreeter {
    use Greeter;
    private const string WHO = 'Jake';
}

Unfortunately PHPStan does not currently take into consideration that self-referencing constants are (usually) meant to be overridden.

This Pull Request omits the Constant Type check for self-referencing constants.

@staabm
Copy link
Contributor

staabm commented Feb 26, 2025

interessting. I have never seen this in practice before.

@ondrejmirtes
Copy link
Member

I've seen this before in the issue tracker but can't find it now. However, this is an awful hack and I don't plan to support it. Your code is broken. Use abstract method instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants