PHP 手冊
前一頁 | 章 15. Operators | 下一頁

# Type Operators

PHP has a single type operator: instanceof. instanceof is used to determine whether a given object is of a specified object class.

The instanceof operator was introduced in PHP 5. Before this time is_a() was used but is_a() has since been deprecated in favor of instanceof.

    <?php
    class A { }
    class B { }

    $thing = new A;

    if ($thing instanceof A) {
        echo 'A';
    }
    if ($thing instanceof B) {
        echo 'B';
    }
    ?>

As $thing is an object of type A, but not B, only the block dependent on the A type will be executed:

    A

See also get_class() and is_a().

前一頁 | 內容 | 下一頁
Array Operators | 上一層 | Control Structures