Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Przekazanie zmiennej z jednej funkcji statycznej do drugiej wewnątrz klasy(?), Łączenie i rozłączanie z bazą danych
Leopard
post
Post #1





Grupa: Zarejestrowani
Postów: 20
Pomógł: 2
Dołączył: 20.07.2008

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


Mam taką klasę:

  1. <?php
  2. class DatabaseManager
  3. {
  4.    private $connection;
  5.    public $registeredUsersNames = array();
  6.    public $registeredUsersEmailes = array();
  7.    
  8.    private static function _getConnection()
  9.    {
  10.        $connection = mysql_connect('localhost', 'root', '')or die(mysql_error());
  11.        mysql_select_db('news')or die(mysql_error());
  12.    }
  13.    
  14.    private static function _closeConnection()
  15.    {
  16.        mysql_close($connection);
  17.        unset($connection);
  18.    }
  19.    
  20.    public static function getRegisteredUsersData()
  21.    {
  22.        self::_getConnection();
  23.        $query = "SELECT name, email FROM users";
  24.        $result = mysql_query($query)or die(mysql_error());
  25.        while($row = mysql_fetch_assoc($result)) {
  26.            $registeredUsersNames[] = $row['name'];
  27.            $registeredUsersEmailes[] = $row['email'];
  28.        }
  29.        self::_closeConnection();
  30.        $data = array_combine($registeredUsersNames, $registeredUsersEmailes);
  31.        return $data;
  32.    }
  33.    
  34.    public static function registerNewUser($name, $pass, $email)
  35.    {
  36.        
  37.    }
  38. }
  39. ?>


Nie wiem co zrobić żeby przekazać zmienną $connection z metody _getConnection do _closeConnection?
$connection w _closeConnection nie istnieje więc nie mogę zamknąć połączenia.

Wszelkie krytyczne uwagi na temat klasy mile widziane (IMG:http://forum.php.pl/style_emoticons/default/guitar.gif)
Go to the top of the page
+Quote Post
Kuzry
post
Post #2





Grupa: Zarejestrowani
Postów: 28
Pomógł: 4
Dołączył: 30.05.2008

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


W funkcji _getConnection zrób tak:
  1. <?php
  2. private static function _getConnection()
  3.   {
  4.       $this->connection = mysql_connect('localhost', 'root', '');
  5.       mysql_select_db('news')or die(mysql_error());
  6.   }
  7. ?>

i wtedy _closeConnection zamykasz tak:
  1. <?php
  2. private static function _closeConnection()
  3.   {
  4.       mysql_close($this->connection);
  5.       unset($connection);
  6.   }
  7. ?>

(IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Ten post edytował Kuzry 6.05.2009, 16:25:13
Go to the top of the page
+Quote Post
Leopard
post
Post #3





Grupa: Zarejestrowani
Postów: 20
Pomógł: 2
Dołączył: 20.07.2008

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


Właśnie sęk w tym, że "$this->" nie działa bo nie tworzę obiektu tej klasy. Wykorzystuję tylko funkcję DatabaseManager::getRegisteredUsersData.
Go to the top of the page
+Quote Post
Lejto
post
Post #4





Grupa: Zarejestrowani
Postów: 1 385
Pomógł: 48
Dołączył: 23.05.2007

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


zasięg zmiennych o to ci chodzi?

Ten post edytował Lejto 6.05.2009, 16:42:09
Go to the top of the page
+Quote Post
Kuzry
post
Post #5





Grupa: Zarejestrowani
Postów: 28
Pomógł: 4
Dołączył: 30.05.2008

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


Fakt nie dopatrzyłem że funkcje są statyczne (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif) Zamiast $this->connect użuj odwołanie self::$connect (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Ten post edytował Kuzry 6.05.2009, 16:40:37
Go to the top of the page
+Quote Post
Leopard
post
Post #6





Grupa: Zarejestrowani
Postów: 20
Pomógł: 2
Dołączył: 20.07.2008

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


Odnośnie zasięgu zmiennych słówko "global" mi nie pomogło.

Z "self::" też nie idzie:
"Fatal error: Access to undeclared static property: DatabaseManager::$connection".
Go to the top of the page
+Quote Post
Kuzry
post
Post #7





Grupa: Zarejestrowani
Postów: 28
Pomógł: 4
Dołączył: 30.05.2008

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


Jak zwykle o czymś zapomnę :] musisz jeszcze do zmiennej dopisać static, czyli:
  1. <?php
  2. class DatabaseManager
  3. {
  4.   private static $connection;
  5.   ...
  6. ?>
Go to the top of the page
+Quote Post
marcio
post
Post #8





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


@Lejto po to jest OOP zeby nie uzywac zmiennych globalnych (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)

Co do tematu to nie mozna np: zrobic tego tak:

  1. <?php
  2. class DatabaseManager
  3. {
  4.    
  5.    public $registeredUsersNames = array();
  6.    public $registeredUsersEmailes = array();
  7.    
  8.    private static function _getConnection()
  9.    {
  10.        mysql_connect('localhost', 'root', '');
  11.        mysql_select_db('news') or die(mysql_error());
  12.    }
  13.    
  14.    private static function _closeConnection()
  15.    {
  16.        mysql_close();
  17.        
  18.    }
  19.    
  20.    public static function getRegisteredUsersData()
  21.    {
  22.        self::_getConnection();
  23.        $query = "SELECT name, email FROM users";
  24.        $result = mysql_query($query)or die(mysql_error());
  25.        while($row = mysql_fetch_assoc($result)) {
  26.            $registeredUsersNames[] = $row['name'];
  27.            $registeredUsersEmailes[] = $row['email'];
  28.        }
  29.        self::_closeConnection();
  30.        $data = array_combine($registeredUsersNames, $registeredUsersEmailes);
  31.        return $data;
  32.    }
  33.    
  34.    public static function registerNewUser($name, $pass, $email)
  35.    {
  36.        
  37.    }
  38. }
  39. ?>

Czy tak by zadzialalo??

To takie moje pytanie.
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: 15.09.2025 - 05:20