The original post: /r/php by /u/k1ll3rM on 2025-01-20 16:59:08.

As far as I know, there’s no way of saying “Trait A can only be used on class B or it’s children”, the only thing you can do is define abstract methods that also exist on class B.

My proposal would be something like:

trait A requires B {}

Where it would trigger an exception if it’s used on a class that doesn’t inherit B. Possibly also with a way to do the same but for interfaces.

Has a solution for this ever been suggested as an RFC? If not, do you think such a feature would be worthwhile?