Check out Scrivo

Do you want to try out Scrivo? Then here's a demo for you that does not just get your feet wet but lets you plunge right in.

Contact us

For more information, please contact us. We're happy to help you out!

Next Oct 4 Previous

Not who: what

The instanceof operator

A: tests if an object implements a specific interface
B: checks if a variable is one of the scalar types string, int or bool
C: can't be used to test if an object class was extended from another base class
D: can't be used on classes that only implement static methods and properties

Answer

The instanceof operator tests if a given object is an instance of the given class and that includes all ancestor classes and interfaces. So you can do things as:

$programmer = new Programmer($jobId);

if ($programmer instanceof Person) { … }

[/code]

or

$filter = new Filter();

if ($filter instanceof Serializable) { … }

[/code]

Therefore answer A and C are correct.

You can't use instanceof on PHP scalar types or arrays, so answer B is false.

Answer D is an answer to trick you. Although it is not very useful to instantiate classes that are implementing static methods or properties only, it very well possible. And so because instances can exist these instances can be tested with instanceof