Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Reflection API - pobieranie metod publicznych.
Forum PHP.pl > Forum > Przedszkole
specialplan
Witam.

W prostym systemie pluginow potrzebuje pobrac nazwy wszystkich metod publicznych danej klasy pluginu.
Mam taka klase:

  1. <?php
  2. class SprockReflection
  3. {
  4.  public $aServices = array();
  5.  private $oReflection;
  6.  
  7.  public function __construct(ReflectionClass $oR)
  8.  {
  9. $this->oReflection = $oR;
  10. foreach ($this->oReflection->getMethods(ReflectionMethod::IS_PUBLIC) as $oM)
  11. {
  12.  $this->aServices[] = $oM;
  13. }
  14.  }
  15.  
  16.  public function __call($method, $args)
  17.  {
  18. if (method_exists($this->oReflection, $method))
  19. {
  20.  return $this->oReflection->{$method}($args);  
  21. }  
  22.  }
  23.  
  24.  /**
  25.  * Get all the public methods from the plugin class and store them in an array
  26.  * Uses Reflection API: ReflectionClass and ReflectionMethod
  27.  * @return array
  28.  */
  29.  public function getServices()
  30.  {
  31. return $this->aServices;
  32.  }  
  33. }
  34. ?>


Ktorej uzywam w sposob nastepujacy:

  1. <?php
  2. $ins = new SprockReflection(new ReflectionClass('news'));
  3.  $a = new ArrayObject($ins->getServices());
  4.  for ($it = $a->getIterator(); $it->valid(); $it->next())
  5.  {
  6. $c = $it->current();
  7. echo $c->getName();  
  8.  }
  9. ?>


I teraz moje pytanie - czy to rozwiazanie jest w miare optymalne? Czy jest prostszy lub lepszy sposob na wyciagniecie nazw kazdej metody publicznej (ale rowniez obiektu)

Pzdr
Sabistik
Tytuł tematu nie spełnia wymagań regulaminu przedszkole. Zamykam. Po wysłaniu na PW poprawnej formy zostanie otworzony.
itsme
temat poprawiony otwieram
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.