Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Wywołanie fukcji z klasy $klasa->funkcja1('value')->funkcja2('value')
Forum PHP.pl > Forum > PHP > Object-oriented programming
gothye
Witam

chce przepisać swoją klasę do obsługi SQL w sposób jaki jest wykorzystywane w Zen

lecz niewiem w jaki sposób zadeklarować dane w klasie abym mógł wykonywać w ten sposób obsługę :

  1.  $users = $sql->select('COUNT(*)')->where('user = 1')->order('ASC');

   


batman
Wystarczy, że zajrzysz do dowolnej klasy ZF i będziesz wiedział.

Wygląda to mniej więcej tak:
  1. class Foo
  2. {
  3. public function bar()
  4. {
  5. // cialo metody
  6. return $this;
  7. }
  8.  
  9. public function baz()
  10. {
  11. // cialo metody
  12. return $this;
  13. }
  14. }
gothye
więc napisałem coś takiego :

  1. class test
  2. {
  3. function __construct()
  4. {
  5.  
  6. }
  7.  
  8. function __set($name,$value)
  9. {
  10. $this->name = $value ;
  11. }
  12.  
  13. function __get($name)
  14. {
  15. return $this->name ;
  16. }
  17.  
  18. public function select($select)
  19. {
  20. return 'SELECT '.$select ;
  21. }
  22.  
  23. function from($table)
  24. {
  25. return $this ;
  26. }
  27.  
  28. function where($where)
  29. {
  30. return 'WHERE '.$where ;
  31. }
  32.  
  33. function order($type)
  34. {
  35.  
  36. }
  37.  
  38. function __destruct()
  39. {
  40.  
  41. }
  42.  
  43. }
  44.  
  45. $test = new test ;
  46. echo $test->select('COUNT(*)')->from('users')->where('User=2');
 


i dostaję :

Fatal error: Call to a member function from() on a non-object in /web/test2.php on line 48

pomijam narazie validacje argumentów w fukcjach sad.gif

MateuszS
  1.  
  2. function from($table)
  3. {
  4. return "FROM".$this ;
  5. }


I obiekt wywołaj

  1. $test = new test();


+ troche bledow w metodach magicznych, ale chyba chciales to pominąć
batman
Select nie zwraca obiektu, tylko string i dlatego masz błąd. Musisz stworzyć właściwość (o typie array), która będzie przechowywała poszczególne elementy zapytania, a kod SQL będzie zwracany w momencie rzutowania obiektu na string (metoda __toString).
gothye
czy o to chodziło ?

  1. class test
  2. {
  3. var $query = array();
  4.  
  5. function __construct()
  6. {
  7.  
  8. }
  9.  
  10. function __set($name,$value)
  11. {
  12. $this->name = $value ;
  13. }
  14.  
  15. function __get($name)
  16. {
  17. return $this->name ;
  18. }
  19.  
  20. public function select($select)
  21. {
  22. $this->query['SELECT'] = $select ;
  23. }
  24.  
  25. public function from($table)
  26. {
  27. $this->query['FROM'] = $table ;
  28. }
  29.  
  30. function where($where)
  31. {
  32. $this->query['WHERE'] = $where ;
  33. }
  34.  
  35. function order($type)
  36. {
  37.  
  38. }
  39.  
  40. function __destruct()
  41. {
  42.  
  43. }
  44.  
  45. }
  46.  
  47. $test = new test() ;
  48. $test->select('COUNT(*)')->from('users')->where('User=2');



Wynik : Fatal error: Call to a member function from() on a non-object sad.gif

erix
select() nie zwraca żadnego obiektu przecież...
MateuszS
podopisuj return $this do select from where (metod) i blad powinien zniknac (mi znikł)
gothye
MateuszScirka wklej proszę kod z poprawkami , zaczynam się już gubić w tym ...




MateuszS
  1. <?php
  2.  
  3. class test
  4. {
  5. var $query = array();
  6.  
  7. function __construct()
  8. {
  9.  
  10. }
  11.  
  12. function __set($name,$value)
  13. {
  14. $this->$name = $value ;
  15. }
  16.  
  17. function __get($name)
  18. {
  19. return $this->$name ;
  20. }
  21.  
  22. public function select($select)
  23. {
  24. $this->query['SELECT'] = $select ;
  25. return $this;
  26. }
  27.  
  28. public function from($table)
  29. {
  30. $this->query['FROM'] = $table ;
  31. return $this;
  32. }
  33.  
  34. function where($where)
  35. {
  36. $this->query['WHERE'] = $where ;
  37. return $this;
  38. }
  39.  
  40. function order($type)
  41. {
  42.  
  43. }
  44.  
  45. function __destruct()
  46. {
  47.  
  48. }
  49.  
  50. }
  51.  
  52. $test = new test() ;
  53. $test->select('COUNT(*)')->from('users')->where('User=2');
  54. ?>
  55.  
thek
Tak jest bo stosujesz "chaining" czyli łańcuch. Zauważ, że na obiekcie this robisz select(). Fajnie i ok... ale Ty OD RAZU chcesz też zrobić metodę from(). Tylko zadaj sobie pytanie "NA JAKIM obiekcie ta metoda działa?". From() w takiej sytuacji bazuje na tym co zwraca select(), bo tak działa "chaining". A że select() NIC nie zwraca to masz problem. Musisz wszystkie funkcje, które w jakikolwiek sposób mogą być użyte jako łańcuchowe, przerobić by zwracały określony obiekt lub wartość. Najlepiej this.
gothye
Dzieki exclamation.gif powoli za zynam pisać walidację danych ,jak skończę zamieszczę efekty na forum .

pozdrawiam

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.