PHP/Reflection/class exists

Материал из Web эксперт
Перейти к: навигация, поиск

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 "

\n

";

foreach ( $interfaces as $interface )

 printf ( "The interface "%s" is %sdefined.
\n", $interface, interface_exists ( $interface, FALSE ) ? "" : "un" );
print "

\n";

?>

 </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>