Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> instancje obiektow - jak to rozumiec, łamigłowka
mrok
post
Post #1





Grupa: Zarejestrowani
Postów: 258
Pomógł: 17
Dołączył: 22.05.2007

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


witam

czegos chyba nie do konca rozumiem i mam prosbę o wyjaśnienie (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Mam kod jak ponizej:

  1. <?php
  2. class DataBase{
  3. private static $AdoDbLiteInstance = null;
  4.  
  5. private function __construct ( )
  6. {
  7. echo 'konstruktor<br>';  //1
  8. self::$AdoDbLiteInstance = NewADOConnection("mysqlt");
  9. self::$AdoDbLiteInstance->Connect($dbHost, $dbUser, $dbPass, $dbName);
  10.  
  11. self::$AdoDbLiteInstance->Execute("SELECT * FROM tabla"); //2
  12. echo self::$AdoDbLiteInstance.'<br>'; //3
  13. }
  14.  
  15. public static function getInstance ( )
  16. {
  17. echo 'getinstance<br>'; //4
  18. if (is_null(self::$AdoDbLiteInstance))
  19. self::$AdoDbLiteInstance = new self();
  20. echo self::$AdoDbLiteInstance.'<br>'; //5
  21. self::$AdoDbLiteInstance->Execute("SELECT * FROM tabla"); //6
  22. return self::$AdoDbLiteInstance;
  23. }
  24. }
  25. ?>


wynikiem dzialania
$db = DataBase::getInstance(); jest

Cytat
getinstance //4
konstruktor //1
Object id #4 //3
Object id #3 //5

Fatal error: Call to undefined method DataBase::Execute() in class/DataBase.class.php on line 32 //6


zmienna $AdoDbLiteInstance jest zmienna statyczna i stale odwoluje sie do niej przez self:: - nie wiem dlaczego wiec raz jest to object4 a raz object3. Ma ktos jakis pomysl?(IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) ?

Ten post edytował mrok 4.03.2008, 22:57:58
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Kocurro
post
Post #2





Grupa: Zarejestrowani
Postów: 461
Pomógł: 32
Dołączył: 17.09.2003
Skąd: Łódź

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


Twój kod:

  1. <?php
  2. class DataBase{
  3. private static $AdoDbLiteInstance = null;
  4.  
  5. private function __construct ( )
  6. {
  7. echo 'konstruktor<br>';  //1
  8. self::$AdoDbLiteInstance = NewADOConnection("mysqlt");
  9. self::$AdoDbLiteInstance->Connect($dbHost, $dbUser, $dbPass, $dbName);
  10.  
  11. self::$AdoDbLiteInstance->Execute("SELECT * FROM tabla"); //2
  12. echo self::$AdoDbLiteInstance.'<br>'; //3
  13. }
  14.  
  15. public static function getInstance ( )
  16. {
  17. echo 'getinstance<br>'; //4
  18. if (is_null(self::$AdoDbLiteInstance))
  19.  new self();
  20. echo self::$AdoDbLiteInstance.'<br>'; //5
  21. self::$AdoDbLiteInstance->Execute("SELECT * FROM tabla"); //6
  22. return self::$AdoDbLiteInstance;
  23. }
  24. }
  25. ?>


Natomiast poprawny kod to byłby np.

  1. <?
  2.  
  3. class DataBase extends
  4. {
  5.  private static $_instance;
  6.  private $_ado;
  7.  
  8.  private function __construct()
  9.  {
  10. $this->_ado = NewADOConnection("mysqlt");
  11. $this->_ado->Connect($dbHost, $dbUser, $dbPass, $dbName);
  12.  
  13. $this->_ado->Execute("SELECT * FROM tabla"); //2
  14.  }
  15.  
  16. public static function getInstance()
  17. {
  18.  
  19. if (is_null(self::$AdoDbLiteInstance))
  20.  self::$AdoDbLiteInstance = new self();
  21. return self::$AdoDbLiteInstance;
  22. }
  23.  
  24. public static function getADO()
  25. {
  26. if (is_null(self::$AdoDbLiteInstance))
  27.  self::$AdoDbLiteInstance = new self();
  28. return self::$AdoDbLiteInstance->_ado;
  29. }
  30. }
  31. ?>


Pisane na szybko z palca by uzmysłowić o co chodzi (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Ogólnie chodzi o to by nie mieszać części statycznej od części dynamicznej. Część dynamiczna nie powinna dotykać części statycznej.

W Twoim przypadku o wiele lepiej byś zrobił jakbyś napisał zamiast swojego taki kod:

  1. <?php
  2. class DataBase{
  3. private static $AdoDbLiteInstance = null;
  4.  
  5. public static function getInstance ( )
  6. {
  7. echo 'getinstance<br>'; //4
  8. if (is_null(self::$AdoDbLiteInstance))
  9. {
  10. self::$AdoDbLiteInstance = NewADOConnection("mysqlt");
  11. self::$AdoDbLiteInstance->Connect($dbHost, $dbUser, $dbPass, $dbName);
  12.  
  13. self::$AdoDbLiteInstance->Execute("SELECT * FROM tabla"); //2  
  14. }
  15. echo self::$AdoDbLiteInstance.'<br>'; //5
  16. self::$AdoDbLiteInstance->Execute("SELECT * FROM tabla"); //6
  17. return self::$AdoDbLiteInstance;
  18. }
  19. }
  20. ?>


Dzięki temu nie tworzysz obiektu do którego nie masz nigdzie uchwytu.

pozdr.

Ten post edytował Kocurro 7.03.2008, 12:17:50
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: 7.10.2025 - 08:13