ReflectionParameter
<<<
ReflectionClass ReflectionMethod
>>>

5.10.18 Réflexion
5.10 Les classes et les objets (PHP 5)
5 Référence du langage
 Manuel PHP

Introduction
ReflectionFunction
ReflectionParameter
->ReflectionClass
ReflectionMethod
ReflectionProperty
ReflectionExtension
Extension des classes de réflexion

5.10.18.4 ReflectionClass

La classe ReflectionClass vous permet de faire du reverse-engineer sur des classes.


<?php
class 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 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(mixed* args)
    
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()
}
?>
Note

hasMethod a été ajouté en PHP 5.1.0.

Pour connaître le fonctionnement d'une classe, vous devez d'abord créer une instance de la classe ReflectionClass . Vous pourrez donc appeler n'importe quelles méthodes de cette instance.

Utilisation de la classe ReflectionClass

<?php
interface Serializable
{
    
// ...
}

class
Object
{
    
// ...
}

/**
* Une classe compteur
*
*/
class Counter extends Object implements Serializable
{
    const
START = 0;
    
private static $c = Counter::START;

    
/**
     * Invocation du compteur
     *
     * @access  public
     * @return  int
     */
    
public function count()
    {
        return
self::$c++;
    }
}

// Création d'une instance de la classe ReflectionClass
$class = new ReflectionClass('Counter');

// Affichage d'informations basiques
printf(
    
"===> La %s%s%s %s '%s' [extension de %s]\n".
    
"     déclarée dans %s\n".
    
"     lignes %d à %d\n".
    
"     a le modificateur %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()))
);

// Affichage du commentaire de la documentation
printf("---> Documentation:\n %s\n", var_export($class->getDocComment(), 1));

// Affichage de l'interface qui implémente cette classe
printf("---> Implémenté :\n %s\n", var_export($class->getInterfaces(), 1));

// Affichage des constantes de la classe
printf("---> Constantes : %s\n", var_export($class->getConstants(), 1));

// Affichage des propriétés de la classe
printf("---> Properties: %s\n", var_export($class->getProperties(), 1));

// Affichage des méthodes de la classe
printf("---> Méthodes : %s\n", var_export($class->getMethods(), 1));

// Si cette classe est instanciable, création d'une instance
if ($class->isInstantiable()) {
    
$counter = $class->newInstance();

    echo
'---> $counter est uneinstance ? ';
    echo
$class->isInstance($counter) ? 'oui' : 'non';

    echo
"\n---> Le nouvel objet Object() est une instance ? ";
    echo
$class->isInstance(new Object()) ? 'oui' : 'non';
}
?>
Note

La méthode newinstance accepte un nombre variable d'arguments passés à la fonction, tout comme la fonction call_user_func .

Note

$class = new ReflectionClass('Foo'); $class->isInstance($arg) est équivalent à $arg instanceof Foo ou is_a($arg, 'Foo') .

<< ReflectionClass >>
ReflectionParameter Réflexion ReflectionMethod