Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Problem z klasami
Matimor
post 2.05.2010, 17:44:50
Post #1





Grupa: Zarejestrowani
Postów: 287
Pomógł: 10
Dołączył: 22.08.2009

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


Witajcie
Mam otóż taki problem, mam zrobioną klasę kod niżej, i teraz po zrobieniu takiej formy jaka jest wywołuje mi błąd, że connect -> query nie istnieje.

  1. class Config
  2. {
  3. var $connect;
  4.  
  5. function cos()
  6. {
  7. $query = $this -> connect -> query();
  8. }
  9. }


Normalnie wyglądało by tak:
  1. class Config
  2. {
  3. function cos()
  4. {
  5. global $connect;
  6. $query = $connect -> query();
  7. }
  8. }



Druga forma działa lecz chcę używać pierwszej, zwracany błąd:
Kod
Fatal error: Call to a member function query() on a non-object in



Pozdrawiam i prosze o pomoc.


--------------------
Go to the top of the page
+Quote Post
Spawnm
post 2.05.2010, 18:08:51
Post #2





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




Nie używaj var , mamy już php5...
Gdzie w 1 ustawiasz wartość $connect ? pokaż więcej kodu.
Go to the top of the page
+Quote Post
Matimor
post 2.05.2010, 18:18:49
Post #3





Grupa: Zarejestrowani
Postów: 287
Pomógł: 10
Dołączył: 22.08.2009

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


  1. $connect = @new mysqli(*, *, *, *);


Zamiast var może być private? $connect to tak samo klasa.


--------------------
Go to the top of the page
+Quote Post
Spawnm
post 2.05.2010, 18:21:39
Post #4





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




Pokaż cały kod...
Jeśli dasz private to będziesz musiał dopisać metodę ustawiającą wartość connect.
Go to the top of the page
+Quote Post
Matimor
post 2.05.2010, 18:25:44
Post #5





Grupa: Zarejestrowani
Postów: 287
Pomógł: 10
Dołączył: 22.08.2009

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


class.connect.php
  1. <?php
  2.  
  3. @require_once("db_mysqli.php");
  4. require_once("settings.php");
  5. $connect = @new mysqli($settings['MySQLHost'], $settings['MySQLUser'], $settings['MySQLPass'], $settings['MySQLDatabase']);
  6. require_once("class.config.php");
  7. ?>


class.config.php
  1. <?php
  2. class Config
  3. {
  4. var $connect;
  5. var $settings = array()
  6.  
  7. function load($name)
  8. {
  9. if($settings[$name] != null)
  10. {
  11. return $settings[$name];
  12. }
  13. else
  14. {
  15. $result = $this -> connect -> query("SELECT `value` FROM `config` WHERE `setting`='{$name}'");
  16. if($result == true)
  17. {
  18. if(($row = $result -> fetch_assoc() != null)
  19. {
  20. return $row[$name];
  21. }
  22. }
  23. }
  24. }
  25. }
  26. ?>


--------------------
Go to the top of the page
+Quote Post
Spawnm
post 2.05.2010, 18:27:41
Post #6





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




Nigdzie nie widzę abyś przekazywał $connect z mysqli do klasy ...
Go to the top of the page
+Quote Post
Matimor
post 2.05.2010, 18:33:50
Post #7





Grupa: Zarejestrowani
Postów: 287
Pomógł: 10
Dołączył: 22.08.2009

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


Hmm głupie pytanie, jak ją przekazać. Nie rozumie Cie.


--------------------
Go to the top of the page
+Quote Post
Spawnm
post 2.05.2010, 18:47:50
Post #8





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




No np. masz:
  1. $connect=new mysql(...);
  2. $cfg=new Config;
  3. $cfg->connect=$connect;
  4. $infp=$cfg->load('cos');
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: 5.07.2025 - 20:46