Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Object type check (instanceof)

Test whether an object belongs to a class or one of its subclasses, or implements an interface.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

$value instanceof SomeInterface

Test whether an object belongs to a class or one of its subclasses, or implements an interface. A nonobject produces false, which makes this check useful at the boundary of an untyped collection. The test includes inherited interfaces; the class name need not exactly match the concrete runtime class.

$value instanceof SomeInterface
[true,false,false]

Examples

<?php
$series = new ArrayIterator([2,3]);
echo json_encode([$series instanceof Traversable, $series instanceof DateTimeInterface, 3 instanceof Traversable]);

Result

[true,false,false]

Sources