Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [singelton] Klasa bazy, .. nie dzila:(
bim2
post
Post #1





Grupa: Zarejestrowani
Postów: 1 873
Pomógł: 152
Dołączył: 9.04.2006
Skąd: Berlin

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


  1. <?php
  2. class mysql {
  3.  
  4. var $db;
  5. var $time_start=0;
  6. var $time_stop=0;
  7. var $time=0;
  8. var $query=0;
  9. var $connect=FALSE;
  10. var $error=array();
  11.  
  12.  
  13. function &getinstance ( $db_host, $db_user, $db_pass, $db_name, $stable = 1 )
  14. {
  15. static $oInstance = false;
  16.  if ( self::$oInstance == false )
  17.  {
  18. self::$oInstance = new Mysql ( $db_host, $db_user, $db_pass, $db_name, $stable );
  19.  }
  20. return self::$oInstance;
  21. }
  22.  
  23. function Mysql( $db_host, $db_user, $db_pass, $db_name, $stable = 1 ) 
  24. {
  25.  
  26. if( $stable )
  27. {
  28. $conn = @mysql_pconnect( $db_host, $db_user, $db_pass );
  29. }
  30. else
  31. {
  32. $conn = @mysql_connect( $db_host, $db_user, $db_pass );
  33. }
  34.  
  35. if ($db = @mysql_select_db($db_name,$conn))
  36. {
  37. $this->db = $db;
  38. $this->connect = TRUE;
  39. return TRUE;
  40. }
  41. else
  42. {
  43. $this->err(mysql_error());
  44. $this->connect = FALSE;
  45. return FALSE;
  46.  
  47. }
  48. }
  49. ?>

mam cos takigo i wywoluje:
  1. <?php
  2. #polaczenie z bazą
  3. $db=& Mysql::getinstance($dbhost,$dbuser,$dbpass,$dbname);
  4. if( !@$db->connect )
  5. {
  6. error_page("2", $db->get_error());
  7. }
  8. ?>
Ale nadal nie działa. Pokzuje sie tylko:
  1. <?php
  2. Parse error: syntax error, unexpected T_IS_EQUAL, expecting '(' in /home/nfs/domains/nfscg.info/public_html/dd/config/db.php on line 18
  3. ?>
CO jest nie tak (IMG:http://forum.php.pl/style_emoticons/default/sad.gif) Mam php4

Pozdrawiam bim
Go to the top of the page
+Quote Post
NuLL
post
Post #2





Grupa: Zarejestrowani
Postów: 2 262
Pomógł: 21
Dołączył: 3.05.2004
Skąd: Sopot, Krakow, W-wa

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


A po co ten self:: skoro instancja to zmienna statyczny metody a nie calej klasy (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)
Go to the top of the page
+Quote Post
bim2
post
Post #3





Grupa: Zarejestrowani
Postów: 1 873
Pomógł: 152
Dołączył: 9.04.2006
Skąd: Berlin

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


Ja tylko robiłem według wzoru:
http://www.strefaphp.net/artykul58.html
Wiec jak to am być?Bo jak daje bez self i jak usune global $db; to sie nie da wywoływać (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
NuLL
post
Post #4





Grupa: Zarejestrowani
Postów: 2 262
Pomógł: 21
Dołączył: 3.05.2004
Skąd: Sopot, Krakow, W-wa

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


Problem w tym ze artykul jest pod php5 (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

  1. <?php
  2. class cos
  3. {
  4. function getInstance()
  5. {
  6. static $_cos;
  7. if($_cos==null)
  8. {
  9. $_cos=&new cos();
  10. }
  11. return $_cos;
  12. }
  13. }
  14. ?>

Dane do polaczenia sciagnij sobie zkas w kontruktorze (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
bim2
post
Post #5





Grupa: Zarejestrowani
Postów: 1 873
Pomógł: 152
Dołączył: 9.04.2006
Skąd: Berlin

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


Thx. Działa ale gdy wywołuje sobie:
  1. <?php
  2. $db=& Mysql::getinstance($dbhost,$dbuser,$dbpass,$dbname);
  3. ?>

a potem mam:
  1. <?php
  2. function tytul() {
  3. global $tbl, $info, $config, $tpl, $onl, $dodatkowe;
  4.  
  5. switch($_GET['x']) 
  6. {
  7. default: echo ERROR.'Staraj sie poruszać po linkach zamieszczonych w serwisie.'.ERROR_END; break;
  8. case 'news':
  9. $sa = "SELECT * FROM $tbl[news] LEFT JOIN $tbl[zrodla] ON $tbl[news].news_zrodlo=$
    tbl[zrodla].zrodla_id WHERE $tbl[news].news_status='0' AND $tbl[news].news_id='{$_GET['x2']}'"
    ;
  10. $result = $db->query($sa);
  11. ?>

I wtedy wyskakuje:

Fatal error: Call to a member function on a non-object in /home/nfs/domains/nfscg.info/public_html/dd/mody/comm/comm.php on line 63

Czyli nie ma obiektu (IMG:http://forum.php.pl/style_emoticons/default/sad.gif) . gdyby zadiałało to nie musiałbym już używać globali a takto globale są tlyko do klas (IMG:http://forum.php.pl/style_emoticons/default/sad.gif)

PS. Na sterfaPHP pisało ze do php 4 też (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Pozdrawiam Bim
Go to the top of the page
+Quote Post

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: 22.08.2025 - 13:05