class_exists

(PHP 4 , PHP 5)

class_exists -- Checks if the class has been defined

Description

bool class_exists ( string class_name [, bool autoload])

Note: The autoload parameter was added in PHP 5

This function returns TRUE if the class given by class_name has been defined, FALSE otherwise.

class_exists() will attempt to call __autoload by default, if you don't want class_exists() to call __autoload(), you can set the parameter autoload to FALSE.

Example 1. autoload parameter example

<?php
    
function __autoload($class) {
        include(
$class . '.php');

        
/* Check to see it the include defined the class */
        
if ( !class_exists($class, false) ) {
           
trigger_error("Unable to load class $class", E_USER_ERROR);
        }
    }
    
    if(
class_exists('MyClass') ) {
        
$myclass = new MyClass();
    }

?>

See also get_declared_classes().