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

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

伊谢尔伦
Release: 2023-03-10 22:54:01
Original
1746 people have browsed it

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){
 //做一些有用的事情
}
Copy after login

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);
?>
Copy after login

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);
?>
Copy after login

Program running result:

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

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);
?>
Copy after login

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!

Related labels:
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