Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Wywołanie fukcji z klasy $klasa->funkcja1('value')->funkcja2('value')
gothye
post
Post #1





Grupa: Zarejestrowani
Postów: 702
Pomógł: 65
Dołączył: 16.03.2009

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


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');

   


Go to the top of the page
+Quote Post
batman
post
Post #2





Grupa: Moderatorzy
Postów: 2 921
Pomógł: 269
Dołączył: 11.08.2005
Skąd: 127.0.0.1




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. }
Go to the top of the page
+Quote Post
gothye
post
Post #3





Grupa: Zarejestrowani
Postów: 702
Pomógł: 65
Dołączył: 16.03.2009

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


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 (IMG:style_emoticons/default/sad.gif)

Go to the top of the page
+Quote Post
MateuszS
post
Post #4





Grupa: Zarejestrowani
Postów: 1 429
Pomógł: 195
Dołączył: 6.10.2008
Skąd: Kraków/Tomaszów Lubelski

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


  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ąć

Ten post edytował MateuszScirka 6.03.2010, 18:02:12
Go to the top of the page
+Quote Post
batman
post
Post #5





Grupa: Moderatorzy
Postów: 2 921
Pomógł: 269
Dołączył: 11.08.2005
Skąd: 127.0.0.1




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).
Go to the top of the page
+Quote Post
gothye
post
Post #6





Grupa: Zarejestrowani
Postów: 702
Pomógł: 65
Dołączył: 16.03.2009

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


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 (IMG:style_emoticons/default/sad.gif)

Go to the top of the page
+Quote Post
erix
post
Post #7





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




select() nie zwraca żadnego obiektu przecież...
Go to the top of the page
+Quote Post
MateuszS
post
Post #8





Grupa: Zarejestrowani
Postów: 1 429
Pomógł: 195
Dołączył: 6.10.2008
Skąd: Kraków/Tomaszów Lubelski

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


podopisuj return $this do select from where (metod) i blad powinien zniknac (mi znikł)
Go to the top of the page
+Quote Post
gothye
post
Post #9





Grupa: Zarejestrowani
Postów: 702
Pomógł: 65
Dołączył: 16.03.2009

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


MateuszScirka wklej proszę kod z poprawkami , zaczynam się już gubić w tym ...




Go to the top of the page
+Quote Post
MateuszS
post
Post #10





Grupa: Zarejestrowani
Postów: 1 429
Pomógł: 195
Dołączył: 6.10.2008
Skąd: Kraków/Tomaszów Lubelski

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


  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.  
Go to the top of the page
+Quote Post
thek
post
Post #11





Grupa: Moderatorzy
Postów: 4 362
Pomógł: 714
Dołączył: 12.02.2009
Skąd: Jak się położę tak leżę :D




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.
Go to the top of the page
+Quote Post
gothye
post
Post #12





Grupa: Zarejestrowani
Postów: 702
Pomógł: 65
Dołączył: 16.03.2009

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


Dzieki (IMG:style_emoticons/default/exclamation.gif) powoli za zynam pisać walidację danych ,jak skończę zamieszczę efekty na forum .

pozdrawiam

Go to the top of the page
+Quote Post

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: 11.06.2026 - 06:53