Mega Code Archive

 
Categories / Php / Reflection
 

Checking for the Existence of Classes and Interfaces Using class_exists() and interface_exists()

<? 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 "<p>"; foreach ( $classes as $class )   printf ( "The class '%s' is %sdefined.<br />\n", $class, class_exists ( $class, FALSE ) ? '' : 'un' ); print "</p>\n<p>"; foreach ( $interfaces as $interface )   printf ( "The interface '%s' is %sdefined.<br />\n", $interface, interface_exists ( $interface, FALSE ) ? '' : 'un' ); print "</p>\n"; ?>