[php-src] Issue #20606: #[\NoDiscard] should affect inheritance and interface implementation
[email protected] (Sharom)
| Newsgroups | php.bugs |
|---|---|
| Message-ID | <lSy5n8dM66pCHHG3pZ2oNZzA0DajLfDGbZD0BrXV24I@main.internal.php.net> |
Issue: https://github.com/php/php-src/issues/20606
Author: Sharom
### Description
`#[\NoDiscard]` attribute added in v8.5 is brilliant, but there is an unexpected nuance.
Using an attribute in an interface method does not affect the implementing class.
The same is true for a child class that overrides the parent method.
In my opinion, this behavior is contrary to common sense.
**For example:**
```php
interface Equatable
{
#[\NoDiscard]
public function equals(object $other): bool;
}
final readonly class IntValue implements Equatable
{
public function __construct(private int $value) {}
#[\Override]
public function equals(object $other): bool
{
return $other instance of static && $this->value == $other->value;
}
}
$x = new IntValue(10);
$y = new IntValue(20);
$x->equals($y); // Warning is naturally expected here,
// but [\NoDiscard] attribute is not "inherited"
// in case of the interface implementation.
```
I may have missed something very important here, let's discuss it.