How to use is_a() function to check if a given object is of this class or of its parent in PHP

1 Answer

0 votes
class test {
      function test() {
         echo "Constructor: class test Constructor <br />";
      }
}
   
class child1 extends test {
      function child1() {
         echo "Constructor: class child1 parent: " . get_parent_class($this) . "<br />";
      }
}
   
$obj = new test();   
if (is_a($obj, 'test')) 
    echo "\$obj is class test<br />";

$c1 = new child1();
if (is_a($obj, 'test')) 
    echo "\$obj parent is class test<br />";

/*
run:
 
Constructor: class test Constructor
$obj is class test
Constructor: class child1 parent: test
$obj parent is class test
    
*/

 



answered Mar 15, 2016 by avibootz
...