On 5/29/23 21:34, Claude Pache wrote:
One weakness of the proposal, is that there is no notice when a method without 
#[\Override] annotation accidentally overrides a parent method. This is 
necessary for the sake of BC, of course. Therefore, (inspired by the 
--noImplicitOverride flag of TypeScript), I suggest adding a complementary 
#[\NoImplicitOverride] annotation on the derived class, that makes #[\Override] 
mandatory on overriding methods. The following example would then trigger a 
compilation error since Laravel 5.4:

Thank you for that suggestion. As a solution to that problem only a per-method attribute came to my mind and that clearly does not scale / adds too much visual noise for a very common case.

However I'm not sure if a class-level attribute is the right choice either. Contrary to #[\Override] itself, which clearly adds metadata (namely the "intent") to a single method, the #[\NoImplicitOverride] is more of a config flag for the engine behavior, instead of "metadata". It doesn't really belong on the class, the class just happens to be a convenient location.

The `declare()` construct however is intended to hold this kind of behavior directives. So `declare(no_implicit_override=1)`, similarly to `declare(strict_types=1)` appears to be the natural solution here. It's not particularly pretty, but would be reasonably consistent with the rest of the language. It would be less forward-compatible (unknown declares emit a Warning), though. I'm also not sure about the impact on the implementation. The support for "no implicit override" would require at least another bit-flag for only a smaller incremental improvement over #[\Override] itself.

Best regards
Tim Düsterhus

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

Reply via email to