Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Reflection API - pobieranie metod publicznych.
specialplan
post 29.02.2008, 12:40:50
Post #1





Grupa: Zarejestrowani
Postów: 206
Pomógł: 21
Dołączył: 1.09.2006
Skąd: Edinburgh

Ostrzeżenie: (0%)
-----


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
Go to the top of the page
+Quote Post
Sabistik
post 29.02.2008, 12:43:20
Post #2


Administrator wortalu


Grupa: Przyjaciele php.pl
Postów: 960
Pomógł: 39
Dołączył: 21.10.2003
Skąd: Kraków

Ostrzeżenie: (0%)
-----


Tytuł tematu nie spełnia wymagań regulaminu przedszkole. Zamykam. Po wysłaniu na PW poprawnej formy zostanie otworzony.
Go to the top of the page
+Quote Post
itsme
post 29.02.2008, 14:08:31
Post #3





Grupa: Zarząd
Postów: 1 512
Pomógł: 2
Dołączył: 22.04.2002
Skąd: Koszalin




temat poprawiony otwieram


--------------------
brak sygnaturki rowniez jest sygnaturką
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.07.2025 - 10:09