Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php] Wywołanie kilku metod, Af::metoda1()->metoda2()->metoda3()
Radeqq
post
Post #1





Grupa: Zarejestrowani
Postów: 15
Pomógł: 2
Dołączył: 10.05.2008

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


Witam

Podpatrzyłem w Kohanie fajny sposób na tworzenie zapytań mysql i chcę mieć podobną obsługę.
  1. class klasa_Mysql {
  2. private $zap;
  3.  
  4. public function select()
  5. {
  6. $this->zap='SELECT '.implode(', ', func_get_args());
  7. return $this;
  8. }
  9.  
  10. public function from(array $tab)
  11. {
  12. echo 'from';
  13. return $this;
  14. }
  15.  
  16. public function join($t1, $znak, $t2)
  17. {
  18. echo 'join';
  19. return $this;
  20. }
  21. }
  22.  
  23. //wywołanie
  24. $a=new klasa_Mysql();
  25. $a->select('t.id', 't.nazwa')->from(array('t_towar', 't1'))->join('t1', '=', 't2');


I to działa ale ja chciałbym wywoływać w taki sposób: (1 linijka)
  1. klasa_Mysql::select('t.id', 't.nazwa')->from(array('t_towar', 't1'))->join('t1', '=', 't2');


Próbowałem zmodyfikować metodę select
  1. private static $zap;
  2.  
  3. public static function select()
  4. {
  5. self::$zap='SELECT '.implode(', ', func_get_args());
  6. return __CLASS__;
  7. }

Ale chyba nie tak jednak. Proszę o pomoc.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Radeqq
post
Post #2





Grupa: Zarejestrowani
Postów: 15
Pomógł: 2
Dołączył: 10.05.2008

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


Ładnie? Teoretycznie mógłbym to upchać w jednej klasie ale tak chyba będzie lepiej?
  1. class klasa_Mysql
  2. {
  3. public static function select()
  4. {
  5. $a=new core_klasa_Mysql();
  6. $a->select(func_get_args());
  7. return $a;
  8. }
  9. }
  10.  
  11. class core_klasa_Mysql {
  12. private $zap;
  13.  
  14. public function select(array $co)
  15. {
  16. $this->zap='SELECT '.implode(', ', $co);
  17. }
  18.  
  19. public function from(array $tab)
  20. {
  21. $this->zap.=' FROM ';
  22. return $this;
  23. }
  24.  
  25. public function join($tab, $typ)
  26. {
  27. $this->zap.=' JOIN ';
  28. return $this;
  29. }
  30.  
  31. public function execute()
  32. {
  33. return $this->zap;
  34. }
  35. }
  36.  
  37. //wywołanie
  38. klasa_Mysql::select('t.id', 't.nazwa', 't.opis', 't.cena', 's.stawka')->from(array('t_towar', 't'))->join('t1', '=', 't2')->execute();
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: 5.10.2025 - 02:39