Skip to main content
adicionou 164 caracteres ao conteúdo; título editado
Fonte Link
Syzoth
  • 100,5mil
  • 23
  • 169
  • 361

Padrão Singleton não funciona em classe do PHPcausa erro: Using $this when not in object context

class Connection
{
    public static $instance;
    private static $dbtype   = "mysql";
    private static $host     = "localhost";
    private static $port     = "3306";
    private static $user     = "root";
    private static $password = "";
    private static $db       = "school";
    
    /*Metodos que trazem o conteudo da variavel desejada
     @return   $xxx = conteudo da variavel solicitada*/
    private static function getDBType()  {return self::$dbtype;}
    private static function getHost()    {return self::$host;}
    private static function getPort()    {return self::$port;}
    private static function getUser()    {return self::$user;}
    private static function getPassword(){return self::$password;}
    private static function getDB()      {return self::$db;}
    
     function __construct() {

    }
    
    public static function getInstance()
    {

     if(!isset(self::$instance)){
         self::$instance = new PDO($this->getDBType().":host=".$this->getHost().";port=".$this->getPort().";dbname=".$this->getDB(), $this->getUser(), $this->getPassword());
            self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            self::$instance->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);

        }
        
        return self::$instance;
    }
    
    private function __clone()
    {
    }
    private function __wakeup()
    {
    }
}

}

Padrão Singleton não funciona em classe do PHP

class Connection
{
public static $instance;
private static $dbtype   = "mysql";
private static $host     = "localhost";
private static $port     = "3306";
private static $user     = "root";
private static $password = "";
private static $db       = "school";

/*Metodos que trazem o conteudo da variavel desejada
 @return   $xxx = conteudo da variavel solicitada*/
private static function getDBType()  {return self::$dbtype;}
private static function getHost()    {return self::$host;}
private static function getPort()    {return self::$port;}
private static function getUser()    {return self::$user;}
private static function getPassword(){return self::$password;}
private static function getDB()      {return self::$db;}

 function __construct() {

}

public static function getInstance()
{

 if(!isset(self::$instance)){
     self::$instance = new PDO($this->getDBType().":host=".$this->getHost().";port=".$this->getPort().";dbname=".$this->getDB(), $this->getUser(), $this->getPassword());
        self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        self::$instance->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);

    }
    
    return self::$instance;
}

private function __clone()
{
}
private function __wakeup()
{
}

}

Padrão Singleton causa erro: Using $this when not in object context

class Connection
{
    public static $instance;
    private static $dbtype   = "mysql";
    private static $host     = "localhost";
    private static $port     = "3306";
    private static $user     = "root";
    private static $password = "";
    private static $db       = "school";
    
    /*Metodos que trazem o conteudo da variavel desejada
     @return   $xxx = conteudo da variavel solicitada*/
    private static function getDBType()  {return self::$dbtype;}
    private static function getHost()    {return self::$host;}
    private static function getPort()    {return self::$port;}
    private static function getUser()    {return self::$user;}
    private static function getPassword(){return self::$password;}
    private static function getDB()      {return self::$db;}
    
     function __construct() {

    }
    
    public static function getInstance()
    {

     if(!isset(self::$instance)){
         self::$instance = new PDO($this->getDBType().":host=".$this->getHost().";port=".$this->getPort().";dbname=".$this->getDB(), $this->getUser(), $this->getPassword());
            self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            self::$instance->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);

        }
        
        return self::$instance;
    }
    
    private function __clone()
    {
    }
    private function __wakeup()
    {
    }
}
Fonte Link
user65509
user65509

Padrão Singleton não funciona em classe do PHP

Fiz esse padrão de Singleton que vi num site e achei ótimo, bem o que queria. Contudo algo está dando errado pois tenho o erro:

Fatal error: Uncaught Error: Using $this when not in object context in /opt/lampp/htdocs/myschool/DAO/Conncetion.php:37
Stack trace: #0 /opt/lampp/htdocs/myschool/Login.php(18):DAO\Connection::getInstance() #1 /opt/lampp/htdocs/myschool/login.php(5):Login->loginconnect() #2 {main} thrown in /opt/lampp/htdocs/myschool
/DAO/Conncetion.php on line 37

E se eu coloco function __construct() {} como privado ou protected ai sim para por esse motivo. Já li sobre o uso do $this num contexto onde não é um objeto. Contudo eu quero usar esse comando por causa da ideia de carregar as váriaveis dinamicamente pelas funções.

Essa é a classe:

class Connection
{
public static $instance;
private static $dbtype   = "mysql";
private static $host     = "localhost";
private static $port     = "3306";
private static $user     = "root";
private static $password = "";
private static $db       = "school";

/*Metodos que trazem o conteudo da variavel desejada
 @return   $xxx = conteudo da variavel solicitada*/
private static function getDBType()  {return self::$dbtype;}
private static function getHost()    {return self::$host;}
private static function getPort()    {return self::$port;}
private static function getUser()    {return self::$user;}
private static function getPassword(){return self::$password;}
private static function getDB()      {return self::$db;}

 function __construct() {

}

public static function getInstance()
{

 if(!isset(self::$instance)){
     self::$instance = new PDO($this->getDBType().":host=".$this->getHost().";port=".$this->getPort().";dbname=".$this->getDB(), $this->getUser(), $this->getPassword());
        self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        self::$instance->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);

    }
    
    return self::$instance;
}

private function __clone()
{
}
private function __wakeup()
{
}

}