PHP/Reflection/class exists
Checking for the Existence of Classes and Interfaces Using class_exists() and interface_exists()
<source lang="html4strict">
<? class Shape {
function __construct($name) { }
} $polly = new Shape ( "A" ); $tweety = new Shape ( "C" ); $square = new Shape ( "S" ); $classes = array ("Parrot", "Canary", "Bird", "Monkey", "Pet" ); $interfaces = array ("Pet", "Product", "Customer", "Bird" );
print "";
foreach ( $classes as $class )
printf ( "The class "%s" is %sdefined.
\n", $class, class_exists ( $class, FALSE ) ? "" : "un" );
print "
";
foreach ( $interfaces as $interface )
printf ( "The interface "%s" is %sdefined.print "\n";
\n", $interface, interface_exists ( $interface, FALSE ) ? "" : "un" );
?>
</source>
Helpful Utility Functions
<source lang="html4strict">
<?
if ($foo = = $bar) { $sam = new Employee; } else { $sam = new Dog; } print "Sam is a " . get_class($sam) . "\n"; print "Class animal exists: " . class_exists("animal") . "\n\n\n\n"; print "All declared classes are: " . get_declared_classes( ) . "\n";
?>
</source>