To test if the class is intantiated or not you can simply do something along the lines of:
try {
if( !class_exists( "Foo" ) )
throw new Exception( "Foo does not exist." );
}catch( Exception $e ){
$e->showMessage();
}
Moreover, if you want to test if it exists when it is actually referenced (and you don't want to do class_exists first) you can do so in an AutoLoader. I've included more on this below, along with an example die statement.
Notes on an AutoLoader
Now, possibly off-topic, if you want to be able to include a class dynamically without explicitly having referenced it, you can by instantiating an AutoLoader. AutoLoaders look for a class file when a class is referenced, and then includes it before trying to use it.
So, for example, you would need to include AutoLoader.php in your Test.php file, as well as every other file using the class. This file would then include other files as needed.
To do this, you need to set up your AutoLoader.php file. It will need to look something like this:
// This tells PHP what to use to load the functions
ini_set( 'unserialize_callback_func', '__autoload' );
// Create an array pairing class names to their file include path
$myClasses = array( "Foo" => "Foo.php", "Bar" => "Bar.php" );
class AutoLoader {
static function autoload( $class ){
if( !in_array( $class, $myClasses ) )
die( "The class {$class} doesn't exist!" );
require( $myClasses[$class] );
}
}
if( function_exists( 'spl_autoload_register' ) ) {
spl_autoload_register( array( 'AutoLoader', 'autoload' ) );
} else {
function __autoload( $className ){
# When called, load a previously unloaded class
AutoLoader::autoload( $className );
}
}