Open In App

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

is_a() Function

The is_a() is a built-in function in PHP which is used to check whether a given object is of a given class or not. It also checks if the given class is one of the parents of the given object or not.

Syntax:



bool is_a( $object, $class_name, $allow_string )

Parameters: This function accepts three parameters as mentioned above and described below:

Return Value : This function returns True if the object is of this class or has this class as one of its parents else it will return a False value.



Below programs illustrate the is_a() function:




<?php 
// PHP program to illustrate the  
// is_a() function 
    
// sample class 
class GeeksforGeeks { 
    var $store = 'Hello geeks!'
    
// create a new object 
$geek = new GeeksforGeeks(); 
    
// checks if $geek is an object 
// of class GeeksforGeeks 
if (is_a($geek, 'GeeksforGeeks')) { 
    echo "Yes"
    
?>

Output:
Yes

instanceof operator

The instanceof operator is used in PHP to find out if an object is an instantiated instance of a class.

Syntax:

$a instanceof MyClass

Operands: This operator contains two operands which are listed below:

Return Value: It returns True if the object is of this class or has this class as one of its parents else it will return a False value.

Below programs illustrate the instanceof operator in PHP:




<?php
// PHP program to illustrate instanceof
// operator
  
// sample class 
class GeeksforGeeks { 
    var $store = 'Hello geeks!'
  
// create a new object 
$geek = new GeeksforGeeks(); 
  
// Checks if $geek is an object of 
// class GeeksforGeeks 
if ($geek instanceof GeeksforGeeks) {
    echo "Yes";
}
  
?>

Output:
Yes

Difference between is_a() function and instanceof operator:


Article Tags :