Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> klopoty ze statycznymi metodami (singleton)
Balin
post
Post #1





Grupa: Zarejestrowani
Postów: 27
Pomógł: 0
Dołączył: 14.05.2003

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


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

Ten post edytował Balin 9.07.2004, 23:46:41
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 1)
Jojo
post
Post #2





Grupa: Zarejestrowani
Postów: 405
Pomógł: 1
Dołączył: 19.09.2003
Skąd: Lublin

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


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.


--------------------
Rozwój cywilizacji informatycznej to wyścig między programistami, którzy wytwarzają coraz łatwiejsze
programy użytkowe dla idiotów, a światem produkującym coraz lepszych gatunkowo idiotów.

Blog: Małolepszy.info
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 Aktualny czas: 21.08.2025 - 09:46