PHP | is_a() function

The is_a() is a built-in function in PHP and 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:

boolean is_a($object, $class)

Parameters: This function accepts two parameters as shown in the above syntax and explained below:



  • $object: The given object to be tested.
  • $class: The name of the class.

Return Type: It returns TRUE if the object given by the parameter $object is of $class or has this $class as one of its parents otherwise it returns FALSE.

Below programs illustrate the is_a() function:

Program 1:

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 = 'geek';
}
  
// 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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate the 
// is_a() function
  
interface parentClass 
{
    public function A();
}
  
class childClass implements parentClass 
{
    public function A () 
    {
        print "A";
    }
}
  
$object = new childClass();
  
if(is_a($object, 'parentClass'))
{
    echo "YES";
}
else
{
    echo "NO";
}
  
?>

chevron_right


Output:

YES

Reference:
http://php.net/manual/en/function.is-a.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.