PHP/Reflection/class exists

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

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";
?>



Helpful Utility Functions

 
<?
    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";
?>