2009-08-17 11 views

Odpowiedz

10

Może Cię zainteresować Reflection API (jeśli to nie jest przesada). W szczególności: -

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

Sprawdź instrukcję, aby uzyskać bardziej szczegółowe przykłady.

+1

Bardziej elastyczne niż get_class_methods, ponieważ można również uzyskać parametry i komentarze. – Kris

+1

Poręczne: 'get_class ($ object);' Kiedy masz do czynienia z wygenerowanym obiektem z wygenerowanej klasy, możesz przekazać to do konstruktora ReflectionClass. Zobacz http://php.net/manual/en/function.get-class.php –

1

Oprócz funkcji wymienionych przez Mathachew można również spojrzeć na Reflection, zwłaszcza na klasę ReflectionClass.

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

Napisałem tę prostą funkcję, która nie tylko wyświetla metod danego obiektu, ale także pokazuje jego właściwości, hermetyzacji i kilka innych przydatnych informacji, takich jak informacje o wydaniu jeśli podano.

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>"; 
      } 

Aby pokazać, jak to działa, stworzyłem losową przykładową klasę. Pozwala utworzyć klasę o nazwie Person i umieść jakieś informacje o wersji tuż powyżej deklaracji klasy:

 /** 
     * 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; 
      } 
     } 

Teraz pozwala utworzyć instancję osoby i owinąć go z naszej funkcji.

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

Ta informacja wyjściowa wola o nazwie klasy, parametrów i metod, w tym o enkapsulacji oraz liczby i nazw parametrów dla każdej metody, metody lokalizacji i linii kodu, w którym istnieje. Zobacz wyjście poniżej:

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 ] 

Mam nadzieję, że okaże się przydatny. Pozdrowienia.

Powiązane problemy