instanceof

PHP instanceof Operator Tips

26th January 2021 - 8 minutes read time

The instanceof operatorĀ in PHP is great at making sure you are looking at a type of object before acting on it. Whilst it's straightforward to use on its own, using it can lead to some undesirable side effects.

As a basic example, let's create a couple of interfaces and classes to show the instanceof operator in action. Rather than just have some test names I am using object names that you might find in a system. This consists of UserĀ and Order that might form part of a commerce system.