Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Przekazanie zmiennej z jednej funkcji statycznej do drugiej wewnątrz klasy(?)
Forum PHP.pl > Forum > PHP > Object-oriented programming
Leopard
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 guitar.gif
Kuzry
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. ?>

winksmiley.jpg
Leopard
Właśnie sęk w tym, że "$this->" nie działa bo nie tworzę obiektu tej klasy. Wykorzystuję tylko funkcję DatabaseManager::getRegisteredUsersData.
Lejto
zasięg zmiennych o to ci chodzi?
Kuzry
Fakt nie dopatrzyłem że funkcje są statyczne tongue.gif Zamiast $this->connect użuj odwołanie self::$connect winksmiley.jpg
Leopard
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".
Kuzry
Jak zwykle o czymś zapomnę :] musisz jeszcze do zmiennej dopisać static, czyli:
  1. <?php
  2. class DatabaseManager
  3. {
  4.   private static $connection;
  5.   ...
  6. ?>
marcio
@Lejto po to jest OOP zeby nie uzywac zmiennych globalnych 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.
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.