PHP type operator

Source: Internet
Author: User

Type operator

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

Example #1 instanceof Use Example

The above routines will output:

BOOL (TRUE) bool (false)

Instanceof can also be used to determine whether a variable is inherited from an instance of a subclass of a parent class: can also be used to determine whether a variable was an instantiated object of a CLA SS that inherits from a parent class:

Example #2 instanceof and Inheritance class example

The above routines will output:

BOOL (TRUE) bool (true)

Check that an object is not an instance of a class, and you can use the logical operator not.

Example #3 use instanceof to check that an object is not an instance of a class

<?phpclass myclass{} $a = new Myclass;var_dump (!) ( $a instanceof StdClass));? >

The above routines will output:

BOOL (TRUE)

Finally, instanceof can also be used to determine if a variable is an instance of an object that implements an interface:

Example #4 Using instanceof for Class<?phpinterface myinterface{}class MyClass implements myinterface{} $a = new MyClass ; Var_dump ($a instanceof MyClass); Var_dump ($a instanceof myinterface);? >

The above routines will output:

BOOL (TRUE) bool (true)

Although instanceof is usually used directly with the class name, you can also use an object or string variable:

Example #5 Using instanceof with other variables<?phpinterface Myinterface{}class MyClass implements myinterface{} $a = New MyClass; $b = new MyClass; $c = ' MyClass '; $d = ' Notmyclass '; Var_dump ($a instanceof $b); $b is an object of class Myclassvar_dump ($a instanceof $c); $c is a string ' MyClass ' var_dump ($a instanceof $d); $d is a string ' Notmyclass '?>

The above routines will output:

BOOL (TRUE) bool (TRUE) bool (false)

However, the use of instanceof has some pitfalls to understand. Before PHP 5.1.0, if the class name to be checked does not exist, instanceof calls __autoload (). In addition, a fatal error occurs if the class is not loaded. You can avoid this problem by using a dynamic class reference (reference) or a string variable that contains the class name:

Example #6 Avoid class name lookups and fatal error problems caused by instanceof in PHP 5.0

<?php$d = ' Notmyclass '; Var_dump ($a instanceof $d); No fatal error here?>

The above routines will output:

BOOL (FALSE)

The instanceof operator was introduced in PHP 5. Prior to this, is_a () was used, but is_a () was obsolete, preferably with instanceof.

Octopus Investment Management May 4 investment advice "Octopus investment": http://licai.daiyuline.com/caijingxinwen/346.html

The rapid rise of the cities in the afternoon prev regained 3,000 points "octopus investment banking": http://licai.daiyuline.com/caijingxinwen/345.html

The combination investment is the financial Management "fund investment Introduction and skill": http://licai.daiyuline.com/jijin/344.html

PHP type operator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.