class_exists: given a class name and check its existance : Reflection Existance « Class « PHP






class_exists: given a class name and check its existance


<?php
class Person {
    private $name;    
    private $age;    
    private $id;    

    function setId( $id ) {
        $this->id = $id;
    }
    
    function getId(){
        echo "get id method";    
    }
    
    function __clone() {
        $this->id = 0;
    }
}

$classname = "Person";
 
if ( ! class_exists( $classname ) ) {
    throw new Exception( "No such class as $classname" );
} 

$myObj = new $classname();
$myObj->getId();
?>

           
       








Related examples in the same category

1.Define class helper class to check the method existance