2009-08-17 14 views

risposta

10

Il Reflection API potrebbe interessarti (se non è eccessivo). In particolare: -

<?php 
    Reflection::export(new ReflectionClass('View')); 
?> 

Consultare il manuale per ulteriori esempi di approfondimento.

+1

Più flessibile di get_class_methods perché puoi anche ottenere parametri e commenti. – Kris

+1

Anche a portata di mano: 'get_class ($ oggetto);' Quando si ha a che fare con un oggetto generato da una classe generata, è possibile passarlo al costruttore di ReflectionClass. Vedere http://php.net/manual/en/function.get-class.php –

1

Oltre alle funzioni citate da Mathachew è anche possibile dare un'occhiata a Reflection, in particolare la classe ReflectionClass.

$class = new ReflectionClass('YourViewClass'); 
$class->getMethods(); 
$class->getProperties(); 
1

Ho scritto questa semplice funzione che non solo mostra le modalità di un determinato oggetto, ma mostra anche le sue proprietà, incapsulamento e alcune altre informazioni utili come note di rilascio se dato.

function TO($object){ //Test Object 
       if(!is_object($object)){ 
        throw new Exception("This is not a Object"); 
        return; 
       } 
       if(class_exists(get_class($object), true)) echo "<pre>CLASS NAME = ".get_class($object); 
       $reflection = new ReflectionClass(get_class($object)); 
       echo "<br />"; 
       echo $reflection->getDocComment(); 

       echo "<br />"; 

       $metody = $reflection->getMethods(); 
       foreach($metody as $key => $value){ 
        echo "<br />". $value; 
       } 

       echo "<br />"; 

       $vars = $reflection->getProperties(); 
       foreach($vars as $key => $value){ 
        echo "<br />". $value; 
       } 
       echo "</pre>"; 
      } 

Per mostrarti come funziona ho creato qualche classe di esempio casuale. Consente di creare una classe denominata Persona e posto alcune note di rilascio appena sopra la dichiarazione di classe:

 /** 
     * DocNotes - This is description of this class if given else it will display false 
     */ 
     class Person{ 
      private $name; 
      private $dob; 
      private $height; 
      private $weight; 
      private static $num; 

      function __construct($dbo, $height, $weight, $name) { 
       $this->dob = $dbo; 
       $this->height = (integer)$height; 
       $this->weight = (integer)$weight; 
       $this->name = $name; 
       self::$num++; 

      } 
      public function eat($var="", $sar=""){ 
       echo $var; 
      } 
      public function potrzeba($var =""){ 
       return $var; 
      } 
     } 

Ora ti permette di creare un'istanza di una persona e avvolgere con la nostra funzione.

$Wictor = new Person("27.04.1987", 170, 70, "Wictor"); 
    TO($Wictor); 

Queste informazioni uscita volontà circa il nome della classe, parametri e metodi, tra cui informazioni incapsulamento e numero ei nomi dei parametri per ciascun metodo, posizione metodo e le righe di codice in cui esiste. Vedere l'output qui sotto:

CLASS NAME = Person 
/** 
      * DocNotes - This is description of this class if given else it will display false 
      */ 

Method [ public method __construct ] { 
    @@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 75 - 82 

    - Parameters [4] { 
    Parameter #0 [ $dbo ] 
    Parameter #1 [ $height ] 
    Parameter #2 [ $weight ] 
    Parameter #3 [ $name ] 
    } 
} 

Method [ public method eat ] { 
    @@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 83 - 85 

    - Parameters [2] { 
    Parameter #0 [ $var = '' ] 
    Parameter #1 [ $sar = '' ] 
    } 
} 

Method [ public method potrzeba ] { 
    @@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 86 - 88 

    - Parameters [1] { 
    Parameter #0 [ $var = '' ] 
    } 
} 


Property [ private $name ] 

Property [ private $dob ] 

Property [ private $height ] 

Property [ private $weight ] 

Property [ private static $num ] 

Spero che lo troverai utile. Saluti.