Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: klopoty ze statycznymi metodami (singleton)
Forum PHP.pl > Forum > PHP > Object-oriented programming
Balin
Mam taki oto singleton:

  1. <?php
  2.  
  3. class App
  4. {
  5. static private $_objInstance = null;
  6.  
  7. public $arrVariables = array();
  8. private $_objAuth = null;
  9.  
  10. private function __construct()
  11. {
  12. $XMLConfigParser = XMLConfigParser::GetInstance();
  13. $_objAuth = new ADODBAuthDriver( $XMLConfigParser->GetMainConfig( 'auth_config' ), $XMLConfigParser->GetMainConfig( 'session_config' ) );
  14. }
  15.  
  16. static function GetInstance()
  17. {
  18. if( !isset( self::$_objInstance ) )
  19. {
  20. self::$_objInstance = new App;
  21. }
  22.  
  23. return self::$_objInstance;
  24. }
  25.  
  26. function SetVariable( $strVariableName, $strVariableValue )
  27. {
  28. $this->arrVariables[$strVariableName] = $strVariableValue;
  29. }
  30. }
  31.  
  32. ?>


Jak widac w onstruktorze tworzony jest nowy egzemplarz klasy ADODBAuthDriver, jednak gdy w srodku tej klasy probuje pobrac instancje obiektu App skrypt nie wykonuje sie (jest tmeout z serwera) przyklad:

  1. <?php
  2.  
  3. class ADODBAuthDriver
  4. {
  5. private $_objDB = null;
  6. private $_objSession = null;
  7.  
  8. private $_strDBUser  = 'root';
  9. private $_strDBPassword = 'pass';
  10. private $_strDBHost  = 'localhost';
  11. private $_strDBName  = 'db';
  12.  
  13. function __construct( $arrAuthConfig, $arrSessionConfig )
  14. {
  15. $this->_objDB = NewADOConnection( 'mysql' );
  16. $this->_objDB->Connect($this->_strDBHost, $this->_strDBUser, $this->_strDBPassword, $this->_strDBName);
  17.  
  18. $this->_objSession = new ADODBSession( $this->_objDB, $arrSessionConfig );
  19.  
  20. // wywolanie App:GetInstance() powoduje timeout
  21. App::GetInstance()->SetVariable( 'objDB', $this->_objDB );
  22. App::GetInstance()->SetVariable( 'objSession', $this->_objSession );
  23. }
  24. }
  25.  
  26. ?>


problem pojawia sie gdy wywoluje metoge ::GetInstance() w obiekcie, ktory zostal stworzony wewnatrz obiektu App, gdy wywoluje App::GetInstance() gdzies na zewnatrz wszystko dziala ok.
Jak myslicie, jaka jest tego przyczyna, czy to jakis bug w php5 rc3 ?

[edit] - poprawione dwukropki
Jojo
Czy te linijki:
  1. <?php
  2. App:GetInstance()->SetVariable( 'objDB', $this->_objDB );
  3. App:GetInstance()->SetVariable( 'objSession', $this->_objSession );
  4. ?>

to tylko błąd przy kopiowaniu, czy przeoczenie przy pisaniu skryptu. Powinny być dwa dwukropki ( :: ) a nie jeden.
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.