Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Filtry i kolejność wykonywania
sf
post
Post #1





Grupa: Zarejestrowani
Postów: 1 597
Pomógł: 30
Dołączył: 19.02.2003
Skąd: Tychy

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


Posiadam klasę HFilters:
  1. <?php
  2. class HFilters
  3. {
  4. private $aFilterList = array();
  5.  
  6.  
  7. private $oContext;
  8.  
  9.  
  10. public function __construct(& $oContext)
  11. {
  12. $this->oContext = & $oContext;
  13. }
  14.  
  15.  
  16. public function add(iFilter $oFilter, $iPreWeight = 10, $iPostWeight = 10)
  17. {
  18. $this->aFilterList[] = $oFilter;
  19. }
  20.  
  21.  
  22. public function pre()
  23. {
  24. foreach($this->aFilterList as $oFilter) {
  25. $oFilter->pre($this->oContext);
  26. }
  27. }
  28.  
  29.  
  30. public function post()
  31. {
  32. foreach($this->aFilterList as $oFilter) {
  33. $oFilter->post($this->oContext);
  34. }
  35. }
  36. }
  37. ?>


IFilter wygląda następująco:
  1. <?php
  2. interface iFilter
  3. {
  4. public function pre($oContext);
  5.  
  6.  
  7. public function post($oContext);
  8. }
  9. ?>


Mam problem z zaimplementowaniem kolejności wykonywania metod pre() i post() w HFilter wg ustalonwej wagi podanej w metodzie add(). Czy ktoś ma jakiś pomysł?

Ten post edytował sf 7.03.2007, 13:30:49
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
sf
post
Post #2





Grupa: Zarejestrowani
Postów: 1 597
Pomógł: 30
Dołączył: 19.02.2003
Skąd: Tychy

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


Implementacja wcale nie jest taka prosta. Ponieważ klucz musi być unikalny, czyli można by to rozwiązać poprzez dodanie iteracyjnego przyrostku, ale to powoduje, że musimy ustalić zakres. Jeśli to będzie liczba złożona z jednej cyfry to mamy ograniczenie do 10 pozycji tylko.

Przy okazji Twój przykład kopiuje dwukrotnie ten sam obiekt. Rozwiązałem całość w następujący sposób.

  1. <?php
  2. class HFilters
  3. {
  4. /** Filters objects list. */
  5. private $aFilters = array();
  6.  
  7.  
  8. /** Pre methods order. */
  9. private $aPreOrder = array();
  10.  
  11.  
  12. /** Post methods order. */
  13. private $aPostOrder = array();
  14.  
  15.  
  16. /** Context objects. */
  17. private $oContext;
  18.  
  19.  
  20. public function __construct($oContext)
  21. {
  22. $this->oContext = & $oContext;
  23. }
  24.  
  25.  
  26. public function add(iFilter $oFilter, $iPreWeight = 10, $iPostWeight = 10)
  27. {
  28. $this->aFilters[] = $oFilter;
  29. $this->aPreOrder[] = $iPreWeight;
  30. $this->aPostOrder[] = $iPostWeight;
  31. }
  32.  
  33.  
  34. public function pre()
  35. {
  36. asort($this->aPreOrder);
  37. foreach($this->aPreOrder as $iKey => $iWeight) {
  38. $this->aFilters[$iKey]->pre($this->oContext);
  39. }
  40. }
  41.  
  42.  
  43. public function post()
  44. {
  45. asort($this->aPostOrder);
  46. foreach($this->aPostOrder as $iKey => $iWeight) {
  47. $this->aFilters[$iKey]->post($this->oContext);
  48. }
  49. }
  50. }
  51.  
  52. ?>


Ten post edytował sf 7.03.2007, 15:54:22
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 18.11.2025 - 15:07