Symfony Doctrine Event Listener Silently Fails on instanceof

If you’ve ever done this in a Symfony doctrine event listener

if ($entity instanceof User) {
  // Do something for this particular entity
}

you might have run across this annoyance.

If you forgot to add a “use” statement for your “User” entity in your event listener class, this check will silently fail. I think PHP would essentially be asking, is $entity an instance of NameofyourBundle/EventListener/User rather than NameofyourBundle/Entity/User. Since $entity is not a NameofyourBundle/EventListener/User, the conditional fails. PHP doesn’t check if NameofyourBundle/EventListener/User exists, so no error is generated. That’s my theory, anyway.

Share Button

Leave a Reply

Your email address will not be published.