Singleton pattern causes error: Using $this when not in Object context

Asked

Viewed 1,442 times

3

I made this Singleton pattern that I saw on a website and I thought it was great, just what I wanted. Yet something is going wrong because I have the mistake:

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

And if I put Function __Construct() {} as private or protected ai yes for that reason. I’ve read about the use of $this in a context where it’s not an object. However I want to use this command because of the idea of dynamically loading the variables by the functions.

This is the class:

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()
    {
    }
}

1 answer

6


getInstance() is static logo has no reference (or cannot access) to attributes of a object then it is not possible to use the $this. Need to change the $this for self either via properties or method (see example)

Change:

self::$instance = new PDO($this->getDBType().":host=".$this->getHost().";port=".$this->getPort().";dbname=".$this->getDB(), $this->getUser(), $this->getPassword());

for:

self::$instance = new PDO(self::getDBType().":host=".self::getHost().";port=".self::getPort().";dbname=".self::getDB(), self::getUser(), self::getPassword());

Using this project pattern for a connection class is not interesting because if the application needs to connect to more than one database will not be possible (even with different data server, user base and password ) because there is already an instantiated connection object. In some cases some opt for the Pattern doubleton, which is basically a Ctrl + C, Ctrl + V of the original class with modified connection data.

Relating:

When to use self vs $this in PHP?

Singleton or class and statistical members?

Singleton standard for database communication

Why shouldn’t we use Singleton?

Avoid permanent connections and Singleton class in a PHP project

  • Hehehe, I just realized it. Show!! + 1

  • Exactly that! Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.