Re: [RFC] [Discussion] #[\Deprecated] for interfaces
| From: | Tim Düsterhus | Date: | Mon, 23 Nov 2026 22:52:32 +0000 |
| Subject: | Re: [RFC] [Discussion] #[\Deprecated] for interfaces | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-133213@lists.php.net to get a copy of this message | ||
Hi
On 2026-11-23 23:25, Daniel Scherzer wrote:
Fixed "type declarations", and added rejected features for the reasoning.Thank you. That mostly looks good to me, but I don't think “Similarly to the handling of instanceof, type declarations do not require specified classes to exist” is quite correct: In case of overridden methods the types need to be available to perform LSP checks:
class Foo {
public function m(): X { }
}
class Bar extends Foo {
public function m(): Y { }
}
prints “Could not check compatibility between Bar::m(): Y and Foo::m(): X, because class Y is not available”
So emitting the warning for parameter/return types could potentially allow for a gradual migration that preserves LSP. A child class could drop the interface from its return type first (and a parent class from parameter types). I don't think this will be particularly common in the real world, though. And of course one would still have the warning when loading classes that implement the deprecated interface (which will happen during object instantiation to return or pass the object in question), so that is probably just sufficient to become aware of the interface being deprecated. And IDEs / Static Analyzers are of course free to provide additional diagnostics.
For completeness: Type declarations also apply to property types (and typed class constants).
Would that be an issue though? If theMy suggestion would thus be to suppress any deprecations “higher” in the type hierarchy if the interface (or in future scope class) is deprecated itself. So the second example would just emit the deprecation for ChildInterface, but not the one for BaseInterface. […]I think this might be a footgun. Consider if the starting situation wasinterface BaseInterface {} #[\Deprecated] interface ChildInterface extends BaseInterface {}where the two interfaces are defined in different libraries, and the ChildInterface is deprecated for unrelated reasons. If the BaseInterface were to become deprecated in the upstream library, the downstream library wouldn't have any warning.
ChildInterface is already deprecated, it is slated to go away either way. So it implementing an interface that then later is deprecated for the rest of its “natural lifetime” might just be okay. Now the base library might perform the actual removal before the child library does and this might result in a breaking change without “heads up” when updating the base library to a new (major) version. But either way it's not really actionable by the child library author, since the child interface is already deprecated and it can't just stop implementing the base interface, since that's a breaking change.
Best regards
Tim Düsterhus