How to use PHP INSTANCEOF

Source: Internet
Author: User

The instanceof operator is introduced in PHP 5. Is_a () was used before, but is_a () is outdated. It is best to use instanceof.

1. Used to determine whether a variable belongs to an instance of a class;

2. It is used to determine whether a variable is an instance that inherits the subclass of a parent class;

3. It is used to determine whether a variable is an instance of an object that implements an interface.


Before PHP 5.1.0, if the class name to be checked does not exist, instanceof will call _ autoload (). In addition, a fatal error occurs if the class is not loaded. You can avoid this problem by using dynamic class reference or using a string variable containing the class name:

The following example illustrates the usage of php instanceof:

If (! Empty ($ current_user )){
If ($ current_user instanceof WP_User)
Return;
 
// Upgrade stdClass to WP_User
If (is_object ($ current_user) & isset ($ current_user-> ID )){
$ Cur_id = $ current_user-> ID;
$ Current_user = null;
Wp_set_current_user ($ cur_id );
Return;
 }
 
// $ Current_user has a junk value. Force to WP_User with ID 0.
$ Current_user = null;
Wp_set_current_user (0 );
Return false;
}

For example, in the highlighted part of the code, if ($ current_user instanceof WP_User), WP_User is a class name. Here, this sentence is used to determine whether $ current_user is an instance of the WP_User class. That is, to determine whether $ current_user = new WP_User () exists in the previous code ().

Use the instanceof keyword to determine whether an object is an instance of a class or a subclass of a class, or whether it implements a specific interface and performs corresponding operations.

Example #6 avoid class name searches and fatal errors caused by instanceof in PHP 5.0

<? Php
$ D = 'notmyclass ';
Var_dump ($ a instanceof $ d); // no fatal error here
?>

Code format: instance name instanceof class name

Use of the instanceof operator

The following example can be run.

<?
Class User {
Private $ name;
Public function getName (){
Return "UserName is". $ this-> name;
 }
}
Class NormalUser extends User {
Private $ age = 99;
Public function getAge (){
Return "age is". $ this-> age;
 }
}
Class UserAdmin {// operation.
Public static function getUserInfo (User $ _ user ){
Echo $ _ user-> getAge ();
 }
}
$ NormalUser = new NormalUser ();
UserAdmin: getUserInfo ($ normalUser );
?>

Program running result:

Age is 99

In the User class, the following error is reported because this method is not available:

<?
Class User {
Private $ name;
Public function getName (){
Return "UserName is". $ this-> name;
 }
}
Class NormalUser extends User {
Private $ age = 99;
Public function getAge (){
Return "age is". $ this-> age;
 }
}
Class UserAdmin {// operation.
Public static function getUserInfo (User $ _ user ){
Echo $ _ user-> getAge ();
 }
}
$ User = new User (); // Here new is User.
UserAdmin: getUserInfo ($ User );
?>

Program running result:

Fatal error: Call to undefined method User: getAge () in E: \ PHPProjects \ NowaMagic \ php \ php_InstanceofOperator.php on line 99

Use the instatnceof operator to ensure code security

Use the instatnceof operator to determine the type before the operation. To ensure code security.

<?
Class User {
Private $ name;
Public function getName (){
Return "UserName is". $ this-> name;
 }
}
Class NormalUser extends User {
Private $ age = 99;
Public function getAge (){
Return "age is". $ this-> age;
 }
}
Class UserAdmin {// operation.
Public static function getUserInfo (User $ _ user ){
If ($ _ user instanceof NormalUser ){
Echo $ _ user-> getAge ();
} Else {
Echo "the type is incorrect. You cannot use this method .";
  }
 }
}
$ User = new User (); // Here new is User.
UserAdmin: getUserInfo ($ User );
?>

Program running result:

The type is incorrect. You cannot use this method.


This is the usage of the php keyword instanceof.

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.