Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> metody statyczne a obiekt innej klasy
become
post
Post #1





Grupa: Zarejestrowani
Postów: 88
Pomógł: 0
Dołączył: 12.01.2007

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


mam taki oto kod:

  1. <?php
  2. class BaseDB {
  3.  var $oDB;
  4.  
  5.  function BaseDB {
  6.  
  7. $DSN="mysql://root:haslo@localhost:3306/newslater";
  8. $oDB=new DB();
  9. $oDB=$oDB->connect($DSN);
  10. if (PEAR::isError($oDB)) {
  11.  $this->$oDB=null;
  12.  return null;
  13. }
  14.  }
  15. }
  16. ?>


  1. <?php
  2. class NewslaterConfig {
  3.  var $mailPerHoure;
  4.  var $fromName;
  5.  var $fromEmail;
  6.  var $charset;
  7.  var $type;
  8.  
  9.  function NewslaterConfig($mPH=null, $fN=null, $fE=null, $ch=null, $type=null) {
  10.  
  11. $this->mailPerHoure=$mPH;
  12. $this->fromName=$fN;
  13. $this->fromEmail=$fE;
  14. $this->charset=$ch;
  15. $this->type=$type;
  16.  }
  17. }
  18.  
  19. class NewslaterConfigDAO extends BaseDB {
  20.  
  21.  static function doLoad () {
  22.  $sQ="
  23. SELECT *
  24. FROM newslater_config
  25. ";
  26. $aRes = $this->oDB->getAssoc($sQ);
  27. if (PEAR::isError($aRes)) {
  28.  #print $aRes->getMessage();
  29.  return NULL;
  30. }
  31. return $this->doReturnFromAssArray($aRes);
  32.  }
  33.  
  34.  
  35.  static function doSave (NewslaterConfig $oConfig) {
  36.  
  37. $sQ="
  38. UPDATE newslater_config
  39. SET `!`=?
  40. ";
  41.  
  42. $stmt=$this->oDB->prepare($sQ);
  43.  
  44. $aData=array(
  45. array("mainperhoure",$oConfig->mailPerHoure),
  46. array("fromname",$oConfig->mailPerHoure),
  47. array("fromemail",$oConfig->mailPerHoure),
  48. array("charset",$oConfig->mailPerHoure),
  49. array("type",$oConfig->type)
  50. );
  51.  
  52. $this->oDB->execute($stmt, $aData);
  53. return 
  54.  }
  55.  
  56.  function doReturnFromAssArray(array $aRes) {
  57.  
  58. return new NewslaterConfig($aRes['mainperhoure'],$aRes['fromname'],$aRes['fromemail'],$aRes['charset'],$aRes['type'])
  59.  
  60.  }
  61.  
  62. }
  63. ?>


chodzi o to, ze tak to nie dziala. nie moge odwołać się poprzez $this w statycznej metodzie.
chcialbym zrobic klase w ktorej beda metody fabrykujace ktore beda korzystac z obiektu DB.

Ten post edytował become 17.01.2008, 12:58:35


--------------------
Darmowy Kreator Stron Internetowych
NR1 W Polsce - Portal Społecznościowo-Randkowy
Pozycjonowanie - najlepsze polskie narzędzie
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 4)
Cezar708
post
Post #2





Grupa: Zarejestrowani
Postów: 1 116
Pomógł: 119
Dołączył: 10.05.2005
Skąd: Poznań

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


  1. <?php
  2. class BaseDB {
  3.  var $oDB;
  4.  
  5.  function BaseDB {
  6.  
  7. $DSN="mysql://root:haslo@localhost:3306/newslater";
  8. $oDB=new DB();
  9. $oDB=$oDB->connect($DSN);
  10. if (PEAR::isError($oDB)) {
  11.  $this->$oDB=null;
  12.  return null;
  13. }
  14. // TU ZABRAKŁO CI TEJ LINII:
  15. $this->$oDB = $oDB;
  16.  }
  17. }
  18. ?>


  1. <?php
  2.  
  3. // (...) opuscilem czesc kodu 
  4.  
  5.  static function doSave (NewslaterConfig $oConfig) {
  6.  
  7. $sQ="
  8. UPDATE newslater_config
  9. SET `!`=?
  10. ";
  11. // więc tutaj obiekt oDB nie mógł być widoczny... 
  12. $stmt=$this->oDB->prepare($sQ);
  13.  
  14. $aData=array(
  15. array("mainperhoure",$oConfig->mailPerHoure),
  16. array("fromname",$oConfig->mailPerHoure),
  17. array("fromemail",$oConfig->mailPerHoure),
  18. array("charset",$oConfig->mailPerHoure),
  19. array("type",$oConfig->type)
  20. );
  21.  // .. podobnie jak tutaj
  22. $this->oDB->execute($stmt, $aData);
  23. return 
  24.  }
  25. // (...)
  26. ?>
Go to the top of the page
+Quote Post
become
post
Post #3





Grupa: Zarejestrowani
Postów: 88
Pomógł: 0
Dołączył: 12.01.2007

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


ale i tak niemogę odwolac sie poprzez $this
próbowalem na rózne sposoby - jak to zrobic ?


--------------------
Darmowy Kreator Stron Internetowych
NR1 W Polsce - Portal Społecznościowo-Randkowy
Pozycjonowanie - najlepsze polskie narzędzie
Go to the top of the page
+Quote Post
Kicok
post
Post #4





Grupa: Zarejestrowani
Postów: 1 033
Pomógł: 125
Dołączył: 17.09.2005
Skąd: Żywiec

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


$oDB też przerób na zmienną statyczną
i zamiast odwoływać się do niej przez: $this->oDB odwołuj się przez: self::$oDB


--------------------
"Sumienie mam czyste, bo nieużywane."
Go to the top of the page
+Quote Post
become
post
Post #5





Grupa: Zarejestrowani
Postów: 88
Pomógł: 0
Dołączył: 12.01.2007

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


tak to nie zadziala

ok zrobilem inaczej.

  1. <?php
  2. class BaseDBSingleton {
  3.  static $oDB=null;
  4.  static $baseType="mysql";
  5.  static $baseCharset="utf8";
  6.  static $resultCharset="utf8";
  7.  static $DSN="mysql://root:haslo@localhost:3306/newsletter";
  8.  
  9.  static function getInstance() {
  10.  
  11. if (is_null(self::$oDB)) {
  12.  self::$oDB = DB::connect(self::$DSN);
  13. }
  14. self::doSetDBCharacter(self::$baseType,self::$baseCharset,self::$resultCharset);
  15. return self::$oDB;
  16.  }
  17.  
  18.  static function doSetDBCharacter($baseType, $baseCharset, $resultCharset) {
  19.  
  20. switch (strtolower($baseType)) {
  21.  case "pgsql":
  22.  $q="SET CLIENT_ENCODING TO '$resultCharset';";
  23.  self::$oDB->query($q);
  24.  break;
  25.  case "mysql":
  26.  $q="SET character_set_client = `$resultCharset`";
  27.  self::$oDB->query($q);
  28.  
  29.  $q="SET character_set_results = `$resultCharset`";
  30.  self::$oDB->query($q);
  31.  
  32.  $q="SET character_set_connection = `$baseCharset`";
  33.  self::$oDB->query($q);
  34.  break;
  35. }
  36.  }
  37. }
  38. ?>


i teraz mam klase DAO

  1. <?php
  2. class NewslaterConfigDAO {
  3.  
  4.  static function doLoad () {
  5.  $sQ="
  6. SELECT *
  7. FROM newslater_config
  8. ";
  9.  
  10. $oDB=BaseDBSingleton::getInstance();
  11. BaseDBSingleton::doSetDBCharacter("mysql","latin2","latin2");
  12.  
  13.  
  14. $aRes = $oDB->getAssoc($sQ);
  15. if (PEAR::isError($aRes)) {
  16.  #print $aRes->getMessage();
  17.  return NULL;
  18. }
  19. return self::doReturnFromAssArray($aRes);
  20.  }
  21. }
  22. ?>


mozecie mi powiedziec dlaczego wywolanie metody:
BaseDBSingleton::doSetDBCharacter("mysql","latin2","latin2");

dziala po wczesniejszym
$oDB=BaseDBSingleton::getInstance();

bo nie rozumiem jakim cudem metoda ta wie z jakiego $oDB ma skorzystac ?


--------------------
Darmowy Kreator Stron Internetowych
NR1 W Polsce - Portal Społecznościowo-Randkowy
Pozycjonowanie - najlepsze polskie narzędzie
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: 20.08.2025 - 18:57