Php Avanzado
Reflection Class
La clase
Reflection Class te permite hacer ingeniería inversa de clases.
<?php
class
Reflection Class 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
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()
}
?>
Nota: hasConstant(), hasMethod(), hasProperty() fueron agregados en PHP 5.1.0.
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
{
const START = 0;
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(
"===> 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 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 = $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';
}
?>