La clase Reflection Class te permite hacer ingeniería inversa de clases.
<?php
class Reflection Class implements Reflector
{
public object __construct(string name)
public string __toString()
public static string export()
public string getName()
public bool isInternal()
public bool isUserDefined()
public bool isInstantiable()
public bool hasConstant(string name)
public bool hasProperty(string name)
public bool hasMethod(string name)
public string getFileName()
public int getStartLine()
public int getEndLine()
public string getDocComment()
public Reflection Method getConstructor()
public Reflection Method getMethod(string name)
public Reflection Method[] getMethods()
public Reflection Property getProperty(string name)
public Reflection Property[] getProperties()
public array getConstants()
public mixed getConstant(string name)
public Reflection Class[] getInterfaces()
public bool isInterface()
public bool isAbstract()
public bool isFinal()
public int getModifiers()
public bool isInstance(stdclass object)
public stdclass newInstance(mixed* args)
public Reflection Class getParentClass()
public bool isSubclassOf(Reflection Class class)
public array getStaticProperties()
public array getDefaultProperties()
public bool isIterateable()
public bool implementsInterface(string name)
public Reflection Extension getExtension()
public string getExtensionName()
?>
Para entender una clase, primero tendrá que crear una instancia de la clase Reflection Class. Entonces puede llamar cualquiera de los métodos anteriores en esta instancia.
Ejemplo 19–35. Usando la clase Reflection Class
<?php
interface Serializable
{
class Object
{
/**
* A counter class
*/
class Counter extends Object implements Serializable
{
private static $c = Counter::START;
* Invoke counter
*
* @access public
* @return int
*/
public function count() {
// Create an instance of the Reflection Class class
$class = new Reflection Class('Counter');
// Print out basic information
printf(
" declared in %s\n" .
" lines %d to %d\n" .
" having the modifiers %d [%s]\n",
$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()))
// Print documentation comment
printf("
> Documentation:\n %s\n", var_export($class->getDocComment(), 1));
// Print which interfaces are implemented by this class
printf("
> Implements:\n %s\n", var_export($class->getInterfaces(), 1));
// Print class constants
printf("
> Constants: %s\n", var_export($class->getConstants(), 1));
// Print class properties
printf("
> Properties: %s\n", var_export($class->getProperties(), 1));
// Print class methods
printf("
> Methods: %s\n", var_export($class->getMethods(), 1));
// If this class is instantiable, create an instance
if ($class->isInstantiable()) {
> $counter is instance? ';
echo $class->isInstance($counter) ? 'yes' : 'no';
> new Object() is instance? ";
echo $class->isInstance(new Object()) ? 'yes' : 'no';
?>