php type operator

伊谢尔伦
Release: 2016-11-24 13:24:30
Original
1447 people have browsed it

instanceof is used to determine whether a PHP variable belongs to an instance of a certain class:

Example #1 Using instanceof

<?php
    class MyClass
    {
    }
    class NotMyClass
    {
    }
    $a = new MyClass;
    var_dump($a instanceof MyClass);
    var_dump($a instanceof NotMyClass);
?>
Copy after login

on a class The above routine will output:

bool(true)
bool(false)

instanceof also Can be used to determine whether a variable is an instance of a subclass inherited from a certain parent class:

Example #2 Use instanceof

<?php
    class ParentClass
    {
    }
    class MyClass extends ParentClass
    {
    }
    $a = new MyClass;
    var_dump($a instanceof MyClass);
    var_dump($a instanceof ParentClass);
?>
Copy after login

on the inherited class The above routine will output:

bool(true)
bool(true)

To check whether an object is not an instance of a class, you can use the logical operator not.

Example #3 Use instanceof to check that the object is not an instance of a certain class

<?php
    class MyClass
    {
    }
    $a = new MyClass;
    var_dump(!($a instanceof stdClass));
?>
Copy after login

The above routine will output:

bool(true)

Finally, instanceof can also be used to determine whether a variable is an object that implements a certain interface Example of:

Example #4 Using instanceof

<?php
    interface MyInterface
    {
    }
    class MyClass implements MyInterface
    {
    }
    $a = new MyClass;
    var_dump($a instanceof MyClass);
    var_dump($a instanceof MyInterface);
?>
Copy after login

on an interface The above routine will output:
bool(true)
bool(true)

Although instanceof is usually used directly with the class name, it can also use objects or characters String variable:

<?php
    interface MyInterface
    {
    }
    class MyClass implements MyInterface
    {
    }
    $a = new MyClass;
    $b = new MyClass;
    $c = &#39;MyClass&#39;;
    $d = &#39;NotMyClass&#39;;
    var_dump($a instanceof $b); // $b is an object of class MyClass
    var_dump($a instanceof $c); // $c is a string &#39;MyClass&#39;
    var_dump($a instanceof $d); // $d is a string &#39;NotMyClass&#39;
?>
Copy after login

The above routine will output:

bool(true)
bool(true)
bool(false)

If the variable being detected is not an object, instanceof does not issue any error message but returns FALSE. Not allowed to detect constants.
Example #6 Use instanceof to detect other variables

<?php
    $a = 1;
    $b = NULL;
    $c = imagecreate(5, 5);
    var_dump($a instanceof stdClass); // $a is an integer
    var_dump($b instanceof stdClass); // $b is NULL
    var_dump($c instanceof stdClass); // $c is a resource
    var_dump(FALSE instanceof stdClass);
?>
Copy after login

The above routine will output:

bool(false)
bool(false)
bool(false)
PHP Fatal error: instanceof expects an object instance, constant given

However There are some pitfalls when using instanceof that you must be aware of. Prior to PHP 5.1.0, instanceof would call __autoload() if the class name to be checked did not exist. Additionally, a fatal error is generated if the class is not loaded. You can avoid this problem by using a dynamic class reference or a string variable containing the class name:

Example #7 Avoid the class name lookup and fatal error problems caused by instanceof in PHP 5.0

<?php
    $d = &#39;NotMyClass&#39;;
    var_dump($a instanceof $d); // no fatal error here
?>
Copy after login

The above routine will output :

bool(false)

instanceof operator was introduced in PHP 5. Before this, is_a() was used, but later is_a() was abandoned and replaced by instanceof. Note that as of PHP 5.3.0, the use of is_a() is restored.


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!