I have seen an example on Stackexchange (please note the trait accessing the class property):
trait CheckPermissionTrait
{
protected function checkPermission($object_id)
{
$judge = $this->container->get('acme_judge');
$user = $this->container->get('security.context')->getToken()->getUser();
if( !$judge->isPermitted($user, $object_id) ) {
throw $this->createAccessDeniedException("Brabbel");
}
}
}
And read one of the repliers comments:
Your trait, then is not a valid use-case: all of its users are required, by definition, to add a $this->container property to its dependencies, which will of course have an impact on that class' contract and the contract of its children.
Why is the author claiming that being a bad use case, if that might be what someone needs? Like if someone has few classes that all have the required dependency and the same logic reccures in all of them, should they just keep the code duplicated?