/** * Inspect modifier. * * @return void */ protected function inspectModifier() { $modifiers = \Reflection::getModifierNames($this->class->getModifiers()); if (!empty($modifiers)) { $this->inspection['modifier'] = implode(' ', $modifiers); } }
public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0) { $prefix = Caster::PREFIX_VIRTUAL; if ($n = \Reflection::getModifierNames($c->getModifiers())) { $a[$prefix.'modifiers'] = implode(' ', $n); } self::addMap($a, $c, array( 'extends' => 'getParentClass', 'implements' => 'getInterfaceNames', 'constants' => 'getConstants', )); foreach ($c->getProperties() as $n) { $a[$prefix.'properties'][$n->name] = $n; } foreach ($c->getMethods() as $n) { $a[$prefix.'methods'][$n->name] = $n; } if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) { self::addExtra($a, $c); } return $a; }
/** * Tests getModifier() method * NB: value is masked because there are many internal constants that aren't exported in the userland * * @dataProvider listOfClasses55 * * @param string $className Class name to test */ public function testGetModifiers($className) { $mask = \ReflectionClass::IS_EXPLICIT_ABSTRACT + \ReflectionClass::IS_FINAL + \ReflectionClass::IS_IMPLICIT_ABSTRACT; $parsedRefClass = $this->parsedRefFileNamespace->getClass($className); $originalRefClass = new \ReflectionClass($className); $parsedModifiers = $parsedRefClass->getModifiers() & $mask; $originalModifiers = $originalRefClass->getModifiers() & $mask; $this->assertEquals($originalModifiers, $parsedModifiers); }
<?php trait T { } $r = new ReflectionClass('T'); var_dump(Reflection::getModifierNames($r->getModifiers())); var_dump($r->isAbstract()); var_dump($r->isInstantiable()); var_dump($r->isCloneable());
{ 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';
var_dump($rb->getInterfaceNames()); print "\n"; print "--- getInterfaces() ---\n"; # Very verbose. #var_dump( $rb->getInterfaces(); print "\n"; print "--- getMethod() ---\n"; print "\n"; print "--- getMethods() ---\n"; # Very verbose. #var_dump( $rb->getMethods(); print "\n"; print "--- getModifiers() ---\n"; var_dump($rb->getModifiers()); print "\n"; print "--- getName() ---\n"; var_dump($rb->getName()); print "\n"; print "--- getParentClass() ---\n"; var_dump($rb->getParentClass()); var_dump($rb->getParentClass()->getName()); print "\n"; print "--- getProperties() ---\n"; # Very verbose. #var_dump( $rb->getProperties(); print "\n"; print "--- getProperty() ---\n"; # Very verbose.
/** * 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}'"); }
/** * Create a syntax highlighted diagram of a class, including parent dependencies, memebers and functions. * * @param object $obj The object to explore. * @return string The formatted diagram of the object. */ public static function classes($obj) { echo self::$is_themed ? '' : self::$theme; $current_class = new \ReflectionObject($obj); $class = new \ReflectionClass($current_class->getName()); $parents = array(); while ($parent = $class->getParentClass()) { $mod = "<h3><b class='rtn'>{$parstr}</b>" . implode(' ', \Reflection::getModifierNames($class->getModifiers())); $nam = "Class <b class='nam'>" . $class->getName() . "</b></h3>"; $lin = self::get_func_url($class->getStartLine(), $class->getEndLine(), $class->getFileName()); $doc = self::clean_docblok($class->getdocComment(), false, "Undocumented {$parstr}" . "Class"); $parents[] = "<div class='method'>"; $parents[] = $lin; $parents[] = Syntax::php(trim($mod . $nam), 'all'); $parents[] = "<b class='com'>{$doc}</b>"; $properties = implode($class->getProperties()); $properties = str_replace('<default>', '', $properties); $properties = str_replace('Property [ ', '', $properties); $properties = str_replace(' ]', '', $properties); $properties = explode(' ', $properties); $len = 0; foreach ($properties as $k => $v) { if ($k % 2 !== 0) { $test = strlen($v); $len = $test > $len ? $test + 1 : $len; } } foreach ($properties as $k => $v) { $properties[$k] = ltrim(sprintf("%-{$len}s", $v)); } $properties = implode('', $properties); $properties = explode("\n", $properties); asort($properties); $properties = "<h4>Properties:</h4>" . implode("\n", $properties); $parents[] = Syntax::php($properties, 'all'); if ($parstr !== 'Parent ') { $parents[] = self::build_func_list($obj); } $parents[] = "</div>"; $parstr = "Parent "; $class = $parent; } $class_info = "Parents: " . implode(", ", $parents); $class_info = Syntax::php($class_info, 'all'); $classinfo = implode($parents); echo $classinfo; /* $class = new \ReflectionObject($obj); ReflectionObject::export($obj); $test = new \ReflectionClass($obj); print_r($test->getProperties()); echo 'getConstants() '; print_r( $class->getConstants() ) .PHP_EOL; echo 'getDefaultProperties()'; print_r( $class->getDefaultProperties() ) .PHP_EOL; echo 'getInterfaceNames() '; print_r( $class->getInterfaceNames() ) .PHP_EOL; echo 'getInterfaces() '; print_r( $class->getInterfaces() ) .PHP_EOL; echo 'getMethods() '; print_r( $class->getMethods() ) .PHP_EOL; echo 'getProperties() '; print_r( $class->getProperties() ) .PHP_EOL; echo 'getStaticProperties() '; print_r( $class->getStaticProperties() ) .PHP_EOL; echo 'getTraitAliases() '; print_r( $class->getTraitAliases() ) .PHP_EOL; echo 'getTraitNames() '; print_r( $class->getTraitNames() ) .PHP_EOL; echo 'getTraits() '; print_r( $class->getTraits() ) .PHP_EOL; echo 'hasConstant() '. AT::boolstr( $class->hasConstant($class->getName())) .PHP_EOL; echo 'inNamespace() '. AT::boolstr( $class->inNamespace() ) .PHP_EOL; echo 'isAbstract() '. AT::boolstr( $class->isAbstract() ) .PHP_EOL; echo 'isCloneable() '. AT::boolstr( $class->isCloneable() ) .PHP_EOL; echo 'isFinal() '. AT::boolstr( $class->isFinal() ) .PHP_EOL; echo 'isInstantiable() '. AT::boolstr( $class->isInstantiable() ) .PHP_EOL; echo 'isInterface() '. AT::boolstr( $class->isInterface() ) .PHP_EOL; echo 'isInternal() '. AT::boolstr( $class->isInternal() ) .PHP_EOL; echo 'isIterateable() '. AT::boolstr( $class->isIterateable() ) .PHP_EOL; echo 'isTrait() '. AT::boolstr( $class->isTrait() ) .PHP_EOL; echo 'isUserDefined() '. AT::boolstr( $class->isUserDefined() ) .PHP_EOL; echo 'getEndLine() '. $class->getEndLine() .PHP_EOL; echo 'getModifiers() '. $class->getModifiers() .PHP_EOL; echo 'getStartLine() '. $class->getStartLine() .PHP_EOL; echo 'getDocComment() '. $class->getDocComment() .PHP_EOL; echo 'getExtensionName() '. $class->getExtensionName() .PHP_EOL; echo 'getFileName() '. $class->getFileName() .PHP_EOL; echo 'getName() '. $class->getName() .PHP_EOL; echo 'getNamespaceName() '. $class->getNamespaceName() .PHP_EOL; echo 'getShortName() '. $class->getShortName() .PHP_EOL; */ }
/** * Returns the Java language modifiers for this class or interface, encoded * in an int. The modifiers consist of the Java Virtual Machine's * constants for <code>public</code>, <code>protected</code>, * <code>private</code>, <code>final</code>, <code>static</code>, * <code>abstract</code> and <code>interface</code>; they should be decoded * using the methods of class <code>Modifier</code>. * * <p> If the underlying class is an array class, then its * <code>public</code>, <code>private</code> and <code>protected</code> * modifiers are the same as those of its component type. If this * <code>Class</code> represents a primitive type or void, its * <code>public</code> modifier is always <code>true</code>, and its * <code>protected</code> and <code>private</code> modifiers are always * <code>false</code>. If this object represents an array class, a * primitive type or void, then its <code>final</code> modifier is always * <code>true</code> and its interface modifier is always * <code>false</code>. The values of its other modifiers are not determined * by this specification. * * <p> The modifier encodings are defined in <em>The Java Virtual Machine * Specification</em>, table 4.1. * * @return the <code>int</code> representing the modifiers for this class * @see java.lang.reflect.Modifier * @since JDK1.1 */ public function getModifiers() { return $this->reflectionClass->getModifiers(); }
<?php class C { } $rc = new ReflectionClass("C"); var_dump($rc->isFinal('X')); var_dump($rc->isInterface(null)); var_dump($rc->isAbstract(true)); var_dump($rc->getModifiers(array(1, 2, 3)));
function transformClass(ReflectionClass $class) { $is_internal = $class->isInternal() ? 'internal' : 'user-defined'; $is_abstract = $class->isAbstract() ? ' abstract' : ''; $is_final = $class->isFinal() ? ' final' : ''; $is_interface = $class->isInterface() ? 'interface' : ''; $type = $class->isInterface() ? 'interface' : 'class'; $name = $class->getName(); $extends = @$class->getParentClass()->name; if ($class->isInternal() || $class->isInterface()) { $this->important[] = $name; } $implements = array(); foreach ($class->getInterfaces() as $i) { $implements[] = $i->name; } $implements_inherited = array(); if ($class->getParentClass()) { foreach ($class->getParentClass()->getInterfaces() as $i) { $implements_inherited[] = $i->name; } } $implements_directly = array_diff($implements, $implements_inherited); $modifiers = Reflection::getModifierNames($class->getModifiers()); extract($this->transformDocBlock($class->getDocComment())); $this->__global_hack = array(); $properties = array(); $properties_inherited = array(); foreach ($class->getProperties() as $property) { if ($property->getDeclaringClass()->name == $class->name) { $properties[] = $this->transformProperty($property, FALSE); } else { $properties_inherited[] = $this->transformProperty($property, TRUE); } } $methods = array(); $methods_inherited = array(); foreach ($class->getMethods() as $method) { if ($method->getDeclaringClass()->name == $class->name) { $methods[] = $this->transformMethod($method); } else { $methods_inherited[] = $this->transformMethod($method); } } ob_start(); include "templates/" . $this->template . "/class.tmpl.php"; return ob_get_clean(); }
/** * @return array */ public function getModifiers() { return Reflection::getModifierNames($this->class->getModifiers()); }
/** * Tests class modifiers. */ public function testModifiers() { static $classes = array('TokenReflection_Test_ClassModifiersIface1', 'TokenReflection_Test_ClassModifiersIface2', 'TokenReflection_Test_ClassModifiersIface3', 'TokenReflection_Test_ClassModifiersIface4', 'TokenReflection_Test_ClassModifiersClass1', 'TokenReflection_Test_ClassModifiersClass2', 'TokenReflection_Test_ClassModifiersClass3', 'TokenReflection_Test_ClassModifiersClass4', 'TokenReflection_Test_ClassModifiersClass5', 'TokenReflection_Test_ClassModifiersClass6', 'TokenReflection_Test_ClassModifiersClass7', 'TokenReflection_Test_ClassModifiersClass8'); require_once $this->getFilePath('modifiers'); $this->getBroker()->process($this->getFilePath('modifiers')); foreach ($classes as $className) { $token = $this->getBroker()->getClass($className); $internal = new \ReflectionClass($className); $this->assertSame($internal->getModifiers(), $token->getModifiers(), $className); } }
function dump_modifierNames($class) { $obj = new ReflectionClass($class); var_dump($obj->getName(), Reflection::getModifierNames($obj->getModifiers())); }
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()) ? 'Да' : 'Нет';
function dump_modifiers($class) { $obj = new ReflectionClass($class); var_dump($obj->getModifiers()); }