Home >Backend Development >PHP Tutorial >What is the difference between is_a() function and instanceof in PHP?

What is the difference between is_a() function and instanceof in PHP?

青灯夜游
青灯夜游Original
2019-03-06 10:06:523436browse

The functions of the is_a() function and instanceof operator in PHP are similar, so what are the differences between them? The following article will give you a brief understanding of the is_a() function and instanceof, and introduce the difference between the is_a() function and instanceof. I hope it will be helpful to you. [Video tutorial recommendation: PHP tutorial]

##PHP is_a() function

is_a() function is a built-in function in PHP that checks whether a given object belongs to a given class; it also checks whether the given class is one of the parent classes of the given object.

Basic syntax:

is_a( $object, $class_name, $allow_string )

Parameters: is_a() function can accept the following three parameters

●Object: Use to save the test object.

●Class_name: used to save the class name.

● Allow_string: If the set value is False, string class names are not allowed to be used as objects.

Return value: If the object belongs to the given class, or this class is one of its parent classes, the is_a() function returns True, otherwise it returns a False value.

Let’s learn about the is_a() function through code examples.

<?php  
class hello {  
    var $store = &#39;Hello PHP!&#39;;  
}  
    
$PHP= new hello();  
    
// 检查$PHP是否是hello类的对象
if (is_a($PHP, &#39;hello&#39;)) {  
    echo "Yes";  
}  
    
?>

Output:

Yes

PHP instanceof operator

Use instanceof operator in PHP to find out whether an object Is an instantiated instance of a class.

Basic syntax:

$a instanceof MyClass

Operands: The instanceof operator contains the following two operands

● $a: represents the object.

● MyClass: Indicates a class name.

Return value: If the object belongs to the given class, or this class is one of its parent classes, it returns True, otherwise it returns a False value.

Let’s learn about the instanceof operator through code examples.

<?php  
class hello {  
    var $store = &#39;Hello PHP!&#39;;  
}  
    
$PHP= new hello();  
    
// 检查$PHP是否是hello类的对象
if ($PHP instanceof hello) {  
    echo "Yes";  
}  
?>

Output:

Yes

The difference between is_a() function and instanceof operator

● is_a() is a function, and instanceof is a language structure. The is_a() function will be significantly slower since it has all the overhead of performing a function call.

●In the case of function callbacks (such as array_map), because instanceof is not a function, it is a language construct, so it cannot be used as a callback. However, callbacks can be used in the is_a() function.

●The usage time of the direct class name in InstanceOf is shorter than that of the is_a() function.

Example:

//语法短(比较)
$a instanceof MyClass
is_a( $a, MyClass::class )

●is_a() is a function that takes an object as parameter 1 and a string as parameter 2, while instanceof takes an object as parameter 1 and can Pass a class name, object instance, or class identifier (class name without quotes) as argument 2.

Example of is_a():

//只有这样才能调用它
is_a($object,$string);

instanceof example:

//对象实例      
$object instanceof $otherObject; 
//字符串类名
$object instanceof $string;
//类的标识符
$object instanceof ClassName;

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of What is the difference between is_a() function and instanceof in PHP?. 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