{ const START = 0; private static $c = Counter::START; /** * Invoke counter * * @access public * @return int */ public function count() { return self::$c++; } } $class = new ReflectionClass('Counter'); printf("===> The %s%s%s %s '%s' [extends %s]" . " declared in %s" . " lines %d to %d" . " having the modifiers %d [%s]<br/>", $class->isInternal() ? 'internal' : 'user-defined', $class->isAbstract() ? ' abstract' : '', $class->isFinal() ? ' final' : '', $class->isInterface() ? 'interface' : 'class', $class->getName(), var_export($class->getParentClass(), 1), $class->getFileName(), $class->getStartLine(), $class->getEndline(), $class->getModifiers(), implode(' ', Reflection::getModifierNames($class->getModifiers()))); // output: ===> The user-defined class 'Counter' [extends ReflectionClass::__set_state(array( 'name' => 'Object', ))] declared in /home/cg/root/main.php lines 15 to 31 having the modifiers 524288 [] printf("===> Documentation:%s<br/>", var_export($class->getDocComment(), 1)); // output: ===> Documentation:'/** * A counter class */' printf("===> Implements:%s<br/>", var_export($class->getInterfaces(), 1)); // output: ===> Implements:array ( 'NSerializable' => ReflectionClass::__set_state(array( 'name' => 'NSerializable', )), ) printf("===> Constants: %s<br/>", var_export($class->getConstants(), 1)); // output: ===> Constants: array ( 'START' => 0, ) printf("===> Properties: %s<br/>", var_export($class->getProperties(), 1)); // output: ===> Properties: array ( 0 => ReflectionProperty::__set_state(array( 'name' => 'c', 'class' => 'Counter', )), ) printf("===> Methods: %s<br/>", var_export($class->getMethods(), 1)); // output: ===> Methods: array ( 0 => ReflectionMethod::__set_state(array( 'name' => 'count', 'class' => 'Counter', )), ) if ($class->isInstantiable()) { $counter = $class->newInstance(); echo '===> $counter is instance? '; echo $class->isInstance($counter) ? 'yes' : 'no';
class Object { } class Counter extends Object implements MyInterface { const START = 0; private static $c = Counter::START; public function count() { return self::$c++; } } // Создание экземпляра класса ReflectionClass $class = new ReflectionClass('Counter'); // Вывод основной информации printf("===> %s%s%s %s '%s' [экземпляр класса %s]\n" . " объявлен в %s\n" . " строки с %d по %d\n", $class->isInternal() ? 'Встроенный' : 'Пользовательский', $class->isAbstract() ? ' абстрактный' : '', $class->isFinal() ? ' финальный' : '', $class->isInterface() ? 'интерфейс' : 'класс', $class->getName(), var_export($class->getParentClass(), 1), $class->getFileName(), $class->getStartLine(), $class->getEndline()); // Вывод тех интерфейсов, которые реализует этот класс printf("---> Интерфейсы:\n %s\n", var_export($class->getInterfaces(), 1)); // Вывод констант класса printf("---> Константы: %s\n", var_export($class->getConstants(), 1)); // Вывод свойств класса printf("---> Свойства: %s\n", var_export($class->getProperties(), 1)); // Вывод методов класса printf("---> Методы: %s\n", var_export($class->getMethods(), 1)); // Если есть возможность создать экземпляр класса, то создаем его if ($class->isInstantiable()) { $counter = $class->newInstance(); echo '---> Создан ли экземпляр класса ' . $class->getName() . '? '; echo $class->isInstance($counter) ? 'Да' : 'Нет'; echo "\n---> Создан ли экземпляр класса Object()? "; echo $class->isInstance(new Object()) ? 'Да' : 'Нет';
/** * Show reflection * * Show reflection * * <code> * Panda_Debug::reflect('BEAR_Form'); // Class * Panda_Debug::reflect($obj); // Objecy * Panda_Debug::reflect('p'); // Function * </code> * * @param string $target target * @param boll $cehckParent check parent class * * @return void */ public static function reflect($target, $cehckParent = false) { if (is_object($target)) { $target = get_class($target); } switch (true) { case function_exists($target): $ref = new ReflectionFunction($target); $info['name'] = $ref->isInternal() ? 'The internal ' : 'The user-defined '; $info['name'] .= $targetName = $ref->getName(); $info['declare in'] = $ref->getFileName() . ' lines ' . $ref->getStartLine() . ' to ' . $ref->getEndline(); $info['Documentation'] = $ref->getDocComment(); $statics = $ref->getStaticVariables(); if ($statics) { $info['Static variables'] = $statics; } $type = 'function'; break; case class_exists($target, false): $ref = new ReflectionClass($target); $type = 'class'; $info['name'] = $ref->isInternal() ? 'The internal ' : 'The user-defined '; $info['name'] .= $ref->isAbstract() ? ' abstract ' : ''; $info['name'] .= $ref->isFinal() ? ' final ' : ''; $info['name'] .= $ref->isInterface() ? 'interface ' : 'class '; $info['name'] .= $targetName = $ref->getName(); $info['declare in'] = $ref->getFileName() . ' lines ' . $ref->getStartLine() . ' to ' . $ref->getEndline(); $info['modifiers'] = Reflection::getModifierNames($ref->getModifiers()); $info['Documentation'] = $ref->getDocComment(); $info['Implements'] = $ref->getInterfaces(); $info['Constants'] = $ref->getConstants(); foreach ($ref->getProperties() as $prop) { // ReflectionProperty クラスのインスタンスを生成する $propRef = new ReflectionProperty($targetName, $prop->name); if ($propRef->isPublic()) { $porps[] = $prop->name; } } // $info['Public Properties'] = $porps; foreach ($ref->getMethods() as $method) { $methodRef = new ReflectionMethod($targetName, $method->name); if ($methodRef->isPublic() || $method->isStatic()) { $final = $method->isFinal() ? 'final ' : ''; $pubic = $method->isPublic() ? 'public ' : ''; $static = $method->isStatic() ? ' static ' : ''; $methods[] = sprintf("%s%s%s %s", $final, $pubic, $static, $method->name); } } $info['Public Methods'] = $methods; if ($ref->isInstantiable() && is_object($target)) { $info['isInstance ?'] = $ref->isInstance($target) ? 'yes' : 'no'; } if ($parent) { $info['parent'] .= $ref->getParentClass(); } break; default: $type = 'Invalid Object/Class'; $targetName = $target; $info = null; break; } print_a($info, "show_objects:1;label: Reflection of {$type} '{$targetName}'"); }
class Object { } class Counter extends Object implements MyInterface { const START = 0; private static $c = Counter::START; public function count() { return self::$c++; } } // Создание экземпляра класса ReflectionClass $class = new ReflectionClass('Counter'); // Вывод основной информации printf("===> %s%s%s %s '%s' [экземпляр класса %s]\n" . " объявлен в %s\n" . " строки с %d по %d\n" . " имеет модификаторы %d [%s]\n", $class->isInternal() ? 'Встроенный' : 'Пользовательский', $class->isAbstract() ? ' абстрактный' : '', $class->isFinal() ? ' финальный' : '', $class->isInterface() ? 'интерфейс' : 'класс', $class->getName(), var_export($class->getParentClass(), 1), $class->getFileName(), $class->getStartLine(), $class->getEndline(), $class->getModifiers(), implode(' ', Reflection::getModifierNames($class->getModifiers()))); // Вывод тех интерфейсов, которые реализует этот класс printf("---> Интерфейсы:\n %s\n", var_export($class->getInterfaces(), 1)); // Вывод констант класса printf("---> Константы: %s\n", var_export($class->getConstants(), 1)); // Вывод свойств класса printf("---> Свойства: %s\n", var_export($class->getProperties(), 1)); // Вывод методов класса printf("---> Методы: %s\n", var_export($class->getMethods(), 1)); // Если есть возможность создать экземпляр класса, то создаем его if ($class->isInstantiable()) { $counter = $class->newInstance(); echo '---> Создан ли экземпляр класса ' . $class->getName() . '? '; echo $class->isInstance($counter) ? 'Да' : 'Нет'; echo "\n---> Создан ли экземпляр класса Object()? "; echo $class->isInstance(new Object()) ? 'Да' : 'Нет';