Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Strict Standards: Non-static method DAO::getInstance() should not be called statically, assuming $this (...)
Forum PHP.pl > Forum > PHP
MenTo(S)
Tak jak w temacie strona sypie errorsami aż miło.
Ktoś wie może gdzie jest błąd ?
Kod
Strict Standards: Non-static method DAO::getInstance() should not be called statically on line 40

    $DAO = DAO::getInstance(); - 40 linijka
    $DAO->connect(CONFIG::DB_ACCOUNTS);
    $DAO->connect(CONFIG::DB_PLAYERS);
    
    
    for($i = 0; $i != count($DAO->dbname); $i++){
        if(!$DAO->isConnected($DAO->dbname[$i])) die('Unable connect to database: '.$DAO->dbname[$i].'<br/><br/>'.($DAO->getConnectException($DAO->dbname[$i])));
    }
    
    $accounts = $DAO->getInstance()->getHandle(CONFIG::DB_ACCOUNTS);
    $players = $DAO->getInstance()->getHandle(CONFIG::DB_PLAYERS);

Strict Standards: Accessing static property DAO::$db as non static in on line 22

Strict Standards: Accessing static property DAO::$db as non static in on line 34

Strict Standards: Accessing static property DAO::$db as non static in on line 29

Strict Standards: Accessing static property DAO::$db as non static in on line 29

        function connect($dbname){            
            $this->dbname[] = $dbname;
            
            $dsn = 'mysql:dbname='.$dbname.';host='.CONFIG::MYSQL_HOST.';port='.CONFIG::MYSQL_PORT;
            try{
                $this->db[$dbname] = new PDO($dsn, CONFIG::MYSQL_USER, CONFIG::MYSQL_PASS); - 22 linijka
            } catch(PDOException $e) {
                $this->connect_exception[$dbname] = $e;                
            }
        }
    
        function getHandle($dbname){
            if(isset($this->db[$dbname])) return $this->db[$dbname]; - 29 linijka
            else throw new Exception('Try get handle to <b>'.$dbname.'</b>. Database <b>'.$dbname.'</b> doesn\'t exists.');
        }
    
        function isConnected($dbname){
            return (is_object($this->db[$dbname])); - 34 linijka
        }
        
        function getConnectException($dbname){
            if(isset($this->connect_exception)){
                return $this->connect_exception[$dbname];
            }
        }

Strict Standards: Non-static method DAO::getInstance() should not be called statically, assuming $this from incompatible context on line 7

Strict Standards: Non-static method DAO::getInstance() should not be called statically, assuming $this from incompatible context on line 7

Strict Standards: Non-static method DAO::getInstance() should not be called statically, assuming $this from incompatible context on line 10

Strict Standards: Non-static method DAO::getInstance() should not be called statically, assuming $this from incompatible context on line 10

        public function __construct() {            
            if(DAO::getInstance()->isConnected(account)) $this->accounts = DAO::getInstance()->getHandle(account); - 7 linijka
            else throw new Exception('Błąd połączenia do bazy danych: '.CONFIG::DB_ACCOUNTS); -7 linijka
            
            if(DAO::getInstance()->isConnected(player)) $this->players = DAO::getInstance()->getHandle(player); - 10 linijka
            else throw new Exception('Błąd połączenia do bazy danych: '.CONFIG::DB_PLAYERS);            -10 linijka
        }
Sephirus
Prawdopodobnie w tej klasie DAO. Metody i własności nie są oznaczone jako STATIC po prostu. Zerknij na kod klasy i pododawaj operator static przy metodzie getInstance() - bo chyba o nią się rozchodzi.

  1.  
  2. public static function getInstance() {
  3. //...
  4. }
MenTo(S)
Cytat(Sephirus @ 9.05.2013, 08:51:42 ) *
Prawdopodobnie w tej klasie DAO. Metody i własności nie są oznaczone jako STATIC po prostu. Zerknij na kod klasy i pododawaj operator static przy metodzie getInstance() - bo chyba o nią się rozchodzi.

  1.  
  2. public static function getInstance() {
  3. //...
  4. }


Jedna kwestia załatwiona zostało jeszcze

private static $db, $instance, $connect_exception;

Kod
Strict Standards: Accessing static property DAO::$db as non static in on line 22

Strict Standards: Accessing static property DAO::$db as non static in on line 34

Strict Standards: Accessing static property DAO::$db as non static in on line 29

Strict Standards: Accessing static property DAO::$db as non static in on line 29

        function connect($dbname){            
            $this->dbname[] = $dbname;
            
            $dsn = 'mysql:dbname='.$dbname.';host='.CONFIG::MYSQL_HOST.';port='.CONFIG::MYSQL_PORT;
            try{
                $this->db[$dbname] = new PDO($dsn, CONFIG::MYSQL_USER, CONFIG::MYSQL_PASS); - 22 linijka
            } catch(PDOException $e) {
                $this->connect_exception[$dbname] = $e;                
            }
        }
    
        function getHandle($dbname){
            if(isset($this->db[$dbname])) return $this->db[$dbname]; - 29 linijka
            else throw new Exception('Try get handle to <b>'.$dbname.'</b>. Database <b>'.$dbname.'</b> doesn\'t exists.');
        }
    
        function isConnected($dbname){
            return (is_object($this->db[$dbname])); - 34 linijka
        }
        
        function getConnectException($dbname){
            if(isset($this->connect_exception)){
                return $this->connect_exception[$dbname];
            }
        }
Sephirus
Wszędzie tam gdzie masz odwołania do tych własności $db, $instance, $connect_exception sprawdź czy nie ma w DAO odwołań poprzez "$this->". Jeśli takowe są zamień $this->wlasnosc na self::$wlasnosc i będzie GIT. Dodatkowo polecam Ci zapoznanie się głębiej z obiektówką w PHP, z mteodami i własnościami statycznymi, tym kiedy stosować $this i self oraz na czym polega singleton.
MenTo(S)
Cytat(Sephirus @ 9.05.2013, 09:11:33 ) *
Wszędzie tam gdzie masz odwołania do tych własności $db, $instance, $connect_exception sprawdź czy nie ma w DAO odwołań poprzez "$this->". Jeśli takowe są zamień $this->wlasnosc na self::$wlasnosc i będzie GIT. Dodatkowo polecam Ci zapoznanie się głębiej z obiektówką w PHP, z mteodami i własnościami statycznymi, tym kiedy stosować $this i self oraz na czym polega singleton.


Serio nie wiem o co chodzi czy mógłbyś zerknąć na DAO?
Całą noc szukałem rozwiązania i już nie mam siły...

  1. <?PHP
  2. class DAO
  3. {
  4. private static $db, $instance, $connect_exception;
  5. public $dbname;
  6.  
  7. function __construct() {}
  8. function __clone() {}
  9.  
  10. public static function getInstance(){
  11. if (self::$instance === null) {
  12. self::$instance = new self();
  13. }
  14. return self::$instance;
  15. }
  16.  
  17. function connect($dbname){
  18. $this->dbname[] = $dbname;
  19.  
  20. $dsn = 'mysql:dbname='.$dbname.';host='.CONFIG::MYSQL_HOST.';port='.CONFIG::MYSQL_PORT;
  21. try{
  22. $this->db[$dbname] = new PDO($dsn, CONFIG::MYSQL_USER, CONFIG::MYSQL_PASS);
  23. } catch(PDOException $e) {
  24. $this->connect_exception[$dbname] = $e;
  25. }
  26. }
  27.  
  28. function getHandle($dbname){
  29. if(isset($this->db[$dbname])) return $this->db[$dbname];
  30. else throw new Exception('Try get handle to <b>'.$dbname.'</b>. Database <b>'.$dbname.'</b> doesn\'t exists.');
  31. }
  32.  
  33. function isConnected($dbname){
  34. return (is_object($this->db[$dbname]));
  35. }
  36.  
  37. function getConnectException($dbname){
  38. if(isset($this->connect_exception)){
  39. return $this->connect_exception[$dbname];
  40. }
  41. }
  42.  
  43. }
  44. ?>
Sephirus
Linijka 22 - zobacz sam. Do własności $db jest odwołanie poprzez "$this->db". Własność $db jest statyczna wg deklaracji z linijki 4 wobec czego powinno się do niej odwoływać poprzez self::$db a nie $this->db.

Trzeba to zamienić. Wszędzie tam gdzie masz:

  1. $this->db
  2. $this->connect_exception


daj zamiast tego:

  1. self::$db
  2. self::$connect_exception


i tyle wink.gif
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.