ReflectionClass

La clase ReflectionClass te permite hacer ingeniería inversa de clases.



<?phpclass ReflectionClass implements Reflector{
    
final private __clone()
    
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 ReflectionMethod getConstructor()
    
public ReflectionMethod getMethod(string name)
    
public ReflectionMethod[] getMethods()
    
public ReflectionProperty getProperty(string name)
    
public ReflectionProperty[] getProperties()
    
public array getConstants()
    
public mixed getConstant(string name)
    
public ReflectionClass[] getInterfaces()
    
public bool isInterface()
    
public bool isAbstract()
    
public bool isFinal()
    
public int getModifiers()
    
public bool isInstance(stdclass object)
    
public stdclass newInstance(mixedargs)
    
public ReflectionClass getParentClass()
    
public bool isSubclassOf(ReflectionClass class)
    
public array getStaticProperties()
    
public array getDefaultProperties()
    
public bool isIterateable()
    
public bool implementsInterface(string name)
    
public ReflectionExtension getExtension()
    
public string getExtensionName()
}
?>


Nota: hasConstant()hasMethod()hasProperty() fueron agregados en PHP 5.1.0.
Para entender una clase, primero tendrá que crear una instancia de la clase ReflectionClass. Entonces puede llamar cualquiera de los métodos anteriores en esta instancia.
Ejemplo 19-35. Usando la clase ReflectionClass
<?php
interface Serializable
{
    
// ...}

class 
Object{
    
// ...}
/**
* A counter class
*/
class Counter extends Object implements Serializable{
    const 
START 0;
    
private static $c Counter::START;

    
/**
     * Invoke counter
     *
     * @access  public
     * @return  int
     */
    
public function count() {
        return 
self::$c++;
    }
}
// Create an instance of the ReflectionClass class$class = new ReflectionClass('Counter');
// Print out basic informationprintf(
    
"===> The %s%s%s %s '%s' [extends %s]\n" .
    
"     declared in %s\n" .
    
"     lines %d to %d\n" .
    
"     having the modifiers %d [%s]\n",
        
$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()))
);
// Print documentation commentprintf("---> Documentation:\n %s\n"var_export($class->getDocComment(), 1));
// Print which interfaces are implemented by this classprintf("---> Implements:\n %s\n"var_export($class->getInterfaces(), 1));
// Print class constantsprintf("---> Constants: %s\n"var_export($class->getConstants(), 1));
// Print class propertiesprintf("---> Properties: %s\n"var_export($class->getProperties(), 1));
// Print class methodsprintf("---> Methods: %s\n"var_export($class->getMethods(), 1));
// If this class is instantiable, create an instanceif ($class->isInstantiable()) {
    
$counter $class->newInstance();

    echo 
'---> $counter is instance? ';
    echo 
$class->isInstance($counter) ? 'yes' 'no';

    echo 
"\n---> new Object() is instance? ";
    echo 
$class->isInstance(new Object()) ? 'yes' 'no';
}
?>

Nota: El método newInstance() acepta un número variable de argumentos los cuales son pasados a la función tal y como si se usara call_user_func().
Nota: $class = new ReflectionClass('Foo'); $class->isInstance($arg) es equivalente a $arg instanceof Foo o is_a($arg, 'Foo').

No response to “ReflectionClass”