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:



  • object: This parameter is used to hold the tested object.
  • class_name: This parameter is used to hold the class name.
  • allow_string: If this parameter set to False, string class name as object is not allowed.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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"
    
?>

chevron_right


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:

  • $a: This is used as an object.
  • MyClass: It is a class name.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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";
}
  
?>

chevron_right


Output:

Yes

Difference between is_a() function and instanceof operator:

  • The is_a() is a function, whereas instanceof is a language construct. The is_a() function will be significantly slower since it has all the overhead of executing a function call.
  • In case of callback in functions (like array_map) instanceof is not a function, it is a language construct, so it cannot use as callback. On the other hand callback can be used in is_a() function.
  • The use of direct class name with instanceof is shorter than is_a() function.
    Example:

    // Short syntax (comparatively)
    $a instanceof MyClass
    
    is_a( $a, MyClass::class )
    
  • The is_a() being a function takes an object as parameter one, and a string as parameter two, whereas instanceof takes an object as parameter one, and can take a class name, object instance, or class identifier (class name written without quotes) as parameter two.

    Example of is_a():

    // Only way to call it
    is_a( $object, $string );
    

    Example of instanceof:

    // Object instance      
    $object instanceof $otherObject; 
    
    // String class name
    $object instanceof $string;
    
    // Identifier for the class
    $object instanceof ClassName;    
    


My Personal Notes arrow_drop_up

A student of Btech Interested in coding and know languages like java C and many more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.