Using $ this when it is not in object context

2

I'm trying to create a class in PHP that connects to the MySQL database and I'm getting the following error:

  

Fatal error: Using $ this when not in object context

ConnectDB.class.php

class ConnectDB
{
    public $pdo;
    private $driver, $host, $port, $base, $user, $pass;

    public function __construct() {
        $this->setDriver('mysql');
        $this->setHost('localhost');
        $this->setPort('3306');
        $this->setBase('bsn');
        $this->setUser('root');
        $this->setPass('');
        $this->connect();
    }

    public function connect() {
        try {
            $this->pdo = new PDO("{$this->getDriver()}:host={$this->getHost()};port={$this->getPort()};dbname={$this->getBase()}", $this->getUser(), $this->getPass());
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
        } catch (PDOException $e) {
            die('Ocorreu um erro na conexão com o banco de dados.');
        }
    }

    public function setDriver($str) {
        $this->driver = $str;
    }

    public function setHost($str) {
        $this->host = $str;
    }

    public function setPort($str)   {
        $this->port = $str;
    }

    public function setBase($str)   {
        $this->base = $str;
    }

    public function setUser($str)   {
        $this->user = $str;
    }

    public function setPass($str)   {
        $this->pass = $str;
    }

    public function getPDO()    {
        return $this->pdo;
    }

    public function getDriver() {
        return $this->driver;
    }

    public function getHost() {
        return $this->host;
    }

    public function getPort() {
        return $this->port;
    }

    public function getBase() {
        return $this->base;
    }

    public function getUser() {
        return $this->user;
    }

    public function getPass() {
        return $this->pass;
    }
}

ManipulateData.class.php

require_once('ConnectDB.class.php');
require_once('ITemplate.php');

class ManipulateData extends ConnectDB implements ITemplate
{
    public function __construct() {
        parent::__construct();
    }

    public static function select($query) {
        $stmt = parent::getPDO()->prepare($query);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    public static function insert($query) {}

    public static function update($query) {}

    public static function delete($query) {}

    public static function doubleData($dados) {}
}  

I'm trying to run my queries like this:

print '<pre>';
print_r(ManipulateData::select('SELECT * FROM vagas'));  

What am I doing wrong?

    
asked by anonymous 24.09.2018 / 22:25

1 answer

7

There is a conceptual error. Almost all the PHP codes I see posted here make this mistake. People do not understand what heritage is and use it anyway. This class should not inherit from the other, it has no relation between them. This is gambiarra. I understand you learned that because almost all PHP codes using OOP are wrong. So it's best not to use OOP. Do not use something you do not master. Tools are only good when you know how to use them. In your case, make it simpler that better code will come out. And in PHP rarely OOP is an advantage, after all it is a scripts language, it does not have the same difficulty as other languages. And this ITemplate seems useless. There are other conceptual problems in the class, even the English name is wrong, but I will not even look at it because its existence is already a mistake. And I did not see much reason to catch an exception just to say it could not connect and end the application. Exception capture should occur to do something useful.

The error is just what was said, it is using $this outside of an object. The $this is the object variable, all methods that access object data have this variable, they are the instance methods. In class methods this variable is not available, so it does not exist, it can not be used. Its methods are all static, ie all class, and have not yet created an instance, so $this is not available. Then it will not work. This works:

class ManipulateData extends ConnectDB {
    public function select($query) {
        $stmt = $this->getPDO()->prepare($query);
        $stmt->execute();
        return $stmt->fetchAll();
    }
}
$obj = new ManipulateData();
print_r($obj->select('SELECT * FROM vagas'));

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

But it's still a very wrong concept. You could just plug into the socket, but you're doing this:

    
24.09.2018 / 22:58