Home  >  Article  >  Backend Development  >  What is the php type operator instanceof? how to use?

What is the php type operator instanceof? how to use?

伊谢尔伦
伊谢尔伦Original
2017-06-21 16:04:141689browse

In PHP5, the type of variables passed through methods is uncertain. Using the instanceof operator, you can determine whether the current instance can have such a form. When the current instance uses instanceof to compare it with the current class, the parent class (infinite tracing upwards), and the implemented interface, it returns true.

"instanceof"The use of operator is very simple. It uses two parameters to complete its function. The first parameter is the object you want to check, and the second parameter is the class name (actually an interface name), used to determine whether this object is an instance of the corresponding class. Of course, the above terminology is used so that you can see how intuitive this operator is to use. Its basic syntax is as follows:

if (object instanceof class name){
 //做一些有用的事情
}

instanceof Application of operator

<?php
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{ //操作.
public static function  getUserInfo(User $_user){
echo $_user->getAge();
}
}
$normalUser = new NormalUser();
UserAdmin::getUserInfo($normalUser);
?>

Program running result: age is 99

Because there is no such method in the User class Error report:

<?php
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{ //操作.
public static function  getUserInfo(User $_user){
echo $_user->getAge();
}
}
$User = new User(); // 这里new的是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 perform type judgment before operation. To ensure the security of the code.

<?php
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{ //操作.
public static function  getUserInfo(User $_user){
if($_user instanceof NormalUser ){
echo $_user->getAge();
}else{
echo "类型不对,不能使用这个方法.";
}
}
}
$User = new User(); // 这里new的是User.
UserAdmin::getUserInfo($User);
?>

Program execution result: The type is wrong and this method cannot be used.

The above is the detailed content of What is the php type operator instanceof? how to use?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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