Source Code:
(back to article)
<?php // Example 1 class MyClass { } class MyOtherClass { } $object = new MyClass(); if ($object instanceof MyClass) { echo "The object is an instance of MyClass."; } else { echo "The object is not an instance of MyClass."; } // Output: The object is an instance of MyClass. // Example 2 class Fruit { } class Apple extends Fruit { } class Banana extends Fruit { } $apple = new Apple(); $banana = new Banana(); if ($apple instanceof Fruit) { echo "The apple is a fruit."; } if ($banana instanceof Fruit) { echo "The banana is a fruit."; } // Output: The apple is a fruit. The banana is a fruit.
Result:
Report an issue