ReflectionFunction::__construct

(PHP 5)

ReflectionFunction::__constructConstruit un nouvel objet ReflectionFunction

Description

public ReflectionFunction::__construct ( mixed $name )

Construit un nouvel objet ReflectionFunction.

Liste de paramètres

name

Le nom de la fonction à refléter ou une fermeture.

Valeurs de retour

Aucune valeur n'est retournée.

Erreurs / Exceptions

Une exception ReflectionException si le paramètre name contient une fonction invalide.

Historique

Version Description
5.3.0 name autorise maintenant les fermetures.

Exemples

Exemple #1 Exemple avec ReflectionFunction::__construct()

  1. <?php
  2. /**
  3. * Un simple compteur
  4. *
  5. * @return int
  6. */
  7. function counter1()
  8. {
  9. static $c = 0;
  10. return ++$c;
  11. }
  12.  
  13. /**
  14. * Une autre simple compteur
  15. *
  16. * @return int
  17. */
  18. $counter2 = function()
  19. {
  20. static $d = 0;
  21. return ++$d;
  22.  
  23. };
  24.  
  25. function dumpReflectionFunction($func)
  26. {
  27. // Affiche des informations basiques
  28. printf(
  29. "\n\n===> The %s function '%s'\n".
  30. " declared in %s\n".
  31. " lines %d to %d\n",
  32. $func->isInternal() ? 'internal' : 'user-defined',
  33. $func->getName(),
  34. $func->getFileName(),
  35. $func->getStartLine(),
  36. $func->getEndline()
  37. );
  38.  
  39. // Affiche les commentaires de documentation
  40. printf("---> Documentation:\n %s\n", var_export($func->getDocComment(), 1));
  41.  
  42. // Affiche les variables statiques existantes
  43. if ($statics = $func->getStaticVariables())
  44. {
  45. printf("---> Static variables: %s\n", var_export($statics, 1));
  46. }
  47. }
  48.  
  49. // Créer une instance de la classe ReflectionFunction
  50. dumpReflectionFunction(new ReflectionFunction('counter1'));
  51. dumpReflectionFunction(new ReflectionFunction($counter2));
  52. ?>

L'exemple ci-dessus va afficher quelque chose de similaire à :

===> The user-defined function 'counter1'
     declared in Z:\reflectcounter.php
     lines 7 to 11
---> Documentation:
 '/**
 * A simple counter
 *
 * @return    int
 */'
---> Static variables: array (
  'c' => 0,
)


===> The user-defined function '{closure}'
     declared in Z:\reflectcounter.php
     lines 18 to 23
---> Documentation:
 '/**
 * Another simple counter
 *
 * @return    int
 */'
---> Static variables: array (
  'd' => 0,
)

Voir aussi

LoadingChargement en cours