PHPEdit.net Community
   
1 2 3 4
Tutorials Tips Pasties Code Snippets
 

Home > Pasties > Pastie #11

Created on 29/02/2008 09:44 by harold03

  1. /**
  2. * cette fonction affiche les méthodes et propriétés d'un objet
    (php5)
  3. * exemple :
  4. * $objet=new objetExemple();
  5. * echo($objet);//appel implicite à __tostring
  6. * @return
  7. */
  8. function __tostring()
  9. {
  10. $reflexion = new ReflectionObject($this);
  11. $chaineRetour = "<br/>
  12. les proprietes de <span style='color:red;'>" . $reflexion->getName()
    . "</span> ont comme valeur :<br/>";
  13.  
  14. foreach($reflexion->getProperties() as $valeur) {
  15. $prop = $reflexion->getProperty($valeur->name);
  16. if (gettype($prop->getValue($this)) == "array") {
  17. $chaineRetour .= "<br/><span style='color:orange'>" .
    $valeur->name . " est un tableau</span> :<br/>";
  18. foreach($prop->getValue($this) as $valeur) {
  19. $chaineRetour .= $valeur->__tostring();
  20. }
  21. } elseif (gettype($prop->getValue($this)) != "object") {
  22. $chaineRetour .= $valeur->name . "(" .
    gettype($prop->getValue($this)) . ") : " . $prop->getValue($this) .
    '<br/>';
  23. } else {
  24. $reflexion2 = new
    ReflectionObject($prop->getValue($this));
  25. $chaineRetour .= "<br/><span style='color:blue'>" .
    $valeur->name . " est un objet</span> :<br/>";
  26. $chaineRetour .=
    $prop->getValue($this)->__tostring();
  27. }
  28. }
  29. return $chaineRetour;
  30. }

 
PHPEdit User Community, © 2008 WaterProof SARL
Powered by PHPEdit