Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Obsługa MySQLi w róznych klasach
cafepl_com
post 12.02.2011, 17:09:15
Post #1





Grupa: Zarejestrowani
Postów: 80
Pomógł: 0
Dołączył: 17.01.2007

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


  1. class MyClass1 extends mysqli
  2. {
  3.  
  4. function __construct() {
  5. parent::__construct('localhost', 'root', '', '');
  6.  
  7. }
  8.  
  9. public function get_rows() {
  10. $res = $this->query('SELECT id FROM strony');
  11. while($row = $res->fetch_array()) {
  12. $result[] = $row;
  13. }
  14. return $result;
  15. }
  16.  
  17. }
  18.  
  19. class MyClass2
  20. {
  21.  
  22. function jojo2() {
  23. $res = MyClass1::query('SELECT id FROM strony');
  24.  
  25. while($row = $res->fetch_array()) {
  26. $result[] = $row;
  27. }
  28. return $result;
  29. }
  30.  
  31. }
  32.  
  33.  
  34. $z = new MyClass2;
  35.  
  36. echo $z->jojo2();



Mam 2 klasy, z czego pierwsza dziedziczy klasę mysqli. Nie ma problemów z obsługą metod mysqli w klasie MyClass1, natomiast problem pojawia się w drugiej klasie, w której tak naprawdę średnio wiem, jak to poprawnie obsługiwać. Powyższy kod zwraca następujący błąd:
  1. Fatal error: mysqli::query() must be derived from MyClass2::query in Unknown on line 0


Jak najlepiej rozwiązać ten problem i móc korzystać z mysqli w obojętnie jakiej klasie?

Ten post edytował cafepl_com 12.02.2011, 17:10:23
Go to the top of the page
+Quote Post
Crozin
post 12.02.2011, 17:24:23
Post #2





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


Do poczytania: dependency injection (tutaj konkretny przykład).
Go to the top of the page
+Quote Post
cafepl_com
post 12.02.2011, 17:29:56
Post #3





Grupa: Zarejestrowani
Postów: 80
Pomógł: 0
Dołączył: 17.01.2007

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


Nie ma prostszego rozwiązania niż iplementowanie interface'ów?
Go to the top of the page
+Quote Post
Crozin
post 12.02.2011, 17:40:12
Post #4





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


Możesz pozbyć się interfejsów i tworzyć sztywne zależności (w oparciu o klasę, nie interfejs):
  1. class MyClass {
  2. protected $db;
  3.  
  4. public function __construct(MySQLi $db) {
  5. $this->db = $db;
  6. }
  7.  
  8. public function doSth() {
  9. $this->db->query('..');
  10. }
  11. }
Go to the top of the page
+Quote Post
cafepl_com
post 12.02.2011, 18:09:41
Post #5





Grupa: Zarejestrowani
Postów: 80
Pomógł: 0
Dołączył: 17.01.2007

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


Wymyśliłem coś takiego, jak poniżej i działa. Zastanawiam się natomiast co Wy uważacie o takim rozwiązaniu smile.gif

  1. class MyClass {
  2.  
  3. static $db;
  4.  
  5. public function __construct() {
  6. MyClass::$db = new mysqli('localhost', 'root', '', '');
  7. }
  8.  
  9. public function get_rows() {
  10. $res = MyClass::$db->query('SELECT id FROM strony');
  11.  
  12. while($row = $res->fetch_array()) {
  13. $result[] = $row;
  14. }
  15. return $result;
  16. }
  17.  
  18. }
  19.  
  20. class MyClass2 {
  21.  
  22. public function get_rows2() {
  23. $res = MyClass::$db->query('SELECT id2 FROM strony');
  24.  
  25. while($row = $res->fetch_array()) {
  26. $result[] = $row;
  27. }
  28. return $result;
  29.  
  30. }
  31.  
  32. }
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: 24.07.2025 - 23:00