[php-src] Issue #20606: #[\NoDiscard] should affect inheritance and interface implementation

From: Date: Fri, 28 Nov 2025 14:17:54 +0000
Subject: [php-src] Issue #20606: #[\NoDiscard] should affect inheritance and interface implementation
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-251317@lists.php.net to get a copy of this message
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.

« previous php.bugs (#251317) next »