Method %s::%s cannot be #[\NoDiscard] error_type¶
Description¶
The #[\NoDiscard] attribute marks a function, method or closure so that the engine emits a warning whenever a caller ignores its return value. A number of magic methods – __clone, __set, __unset, __wakeup and __unserialize – are expected to always behave as if declared : void: PHP calls them purely for their side effect and never looks at, or exposes, any value they might return. Marking one of them #[\NoDiscard] is therefore meaningless, and PHP rejects it at compile time.
This is the same wording as the message used for __construct and __destruct (see method-%s::%s-cannot-be-#[–nodiscard]), but produced by a different internal check: those two magic methods are not even allowed to declare a return type at all, whereas the methods covered here are simply always treated as implicitly void. The trailing ‘error_type’ in this file’s identifier is an artifact of how this catalog extracts messages from the PHP source; it is not part of the text PHP actually displays.
Example¶
<?php
class X {
#[\NoDiscard]
public function __clone() {}
}
$x = new X();
clone $x;
?>
Literal Examples¶
Method X::__clone cannot be #[NoDiscard]
Method X::__set cannot be #[NoDiscard]
Method X::__unset cannot be #[NoDiscard]
Method X::__wakeup cannot be #[NoDiscard]
Method X::__unserialize cannot be #[NoDiscard]
Solutions¶
Remove the
#[\NoDiscard]attribute from the magic method.If a discardable-value warning is needed, move the logic to a regular, non-magic method and mark that one instead.
Changed Behavior¶
This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.