Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

instanceof

instanceof is used to determine whether a PHP variable is an instantiated object of a certain class. It is related to the function is_a().

instanceof has a higher precedence than ! operator, so it is applied before the latter. In case of confusion, use parenthesis.

instanceof does not check that the checked class exists: it just checks that the provided object match the class name. If a typo happens in the class name or the class does not exists, the operator fails every time.

<?php

    if ($user instanceof Administrator) {
        $user->admin();
    }
    
    // If The user is not of class User...
    if ( !$user instanceof User ) {
    //if ( !($user instanceof User) ) {
        $user->admin();
    }
    
    var_dump($user instanceof UndefinedClass); // false

?>

Documentation