R
R
Roman2019-05-15 12:38:54
PHP
Roman, 2019-05-15 12:38:54

How to work with PDO inside a class?

Hello.
I master PDO in parallel with OOP.
I created a DB class, in which I registered the following settings:

namespace App\Core;

use PDO;

class DB {
    protected $host;
    protected $user;
    protected $password;
    protected $dbname;
    protected $charset;
    protected $dsn;
    protected $opt;
    

    public function __construct() {
        $this->host = "localhost";
        $this->user = "root";
        $this->password = "";
        $this->dbname = "dbtest";
        $this->charset = "utf8";
        
        $this->dsn = "mysql:host=$this->host;dbname=$this->dbname;charset=$this->charset";
        
        $this->opt = [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES => FALSE
        ];
        
        $pdo = new PDO($this->dsn, $this->user, $this->password, $this->opt);
    }
    
    public function SELECT($Query) {
        $stmt = $pdo->query("$Query");
    }

    public function __destruct() {
        $pdo = NULL;
    }
}

Before that, I made an autoload function so as not to include a bunch of files:
function classLoad($Class) {
    include $_SERVER["DOCUMENT_ROOT"]."/".mb_strtolower($Class).".php";
}

spl_autoload_register("classLoad");

Then, in another class, inherited the DB class and tried to create a method using a method from DB:
namespace App;
use App\Core;

class Settings extends Core\DB {
    
    public function getSiteName() {
        $this->SELECT("SELECT Value FROM configs WHERE Name = 'SiteName'");
        while ($result = $this->SELECT()->fetch()) {
            echo $result["Value"];
        }
    }
}

And the index page, where, in theory, the desired method should be called:
require_once $_SERVER["DOCUMENT_ROOT"]."/autoload.php";

$TEST = new App\Settings();
$TEST->getSiteName();

However, when I tried to display this method, the browser gave the following error:
5cdbe21951b31956685733.png
I don’t know PHP very well yet, so I admit the option that most likely I could make a mistake somewhere (99.9%))))
In general, I will I'm glad if you tell me what mistakes I managed to make, and also show in which direction to dig in order to fix them.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2019-05-15
@Ramapriya

You don't need to learn everything at once. It won't work for you all at once. And you won't know what it is.
. And you have both OOP and PDO, and namespaces, and autoload, and you don’t understand any of these things.
Are you going to write a class for working with the database? Great , write a class to work with the database. WITHOUT namespaces and autoloads. Do not break, add one inclusion. But at least you won’t run through the code and look for which of the 10 places you have an error in.
Only after the class is working for you, you add namespaces. And you are already suffering with them.
After you start working with namespaces, you master autoload.
Your bike, so that it is at least good for something and adds at least something to the original PDO, you rewrite it like this

class DB
{
    public $pdo;

    public function __construct() {
        $host = "localhost";
        $user = "root";
        $password = "";
        $dbname = "dbtest";
        $charset = "utf8";
        
        $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
        $this->opt = [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES => FALSE
        ];
        $this->pdo = new PDO($dsn, $user, $password, $opt);
    }
    
    public function query($sql, $args = NULL)
    {
        if (!$args)
        {
             return $this->pdo->query($sql);
        }
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($args);
        return $stmt;
    }
    public function __destruct() {
        $pdo = NULL;
    }
}

Code Explanations (in English) - Childhood diseases of my first class for working with the DB
Next.
The Settings class must not inherit from the DB class. These are completely different entities. The human class should not inherit from the Pocket class just because all humans have pockets normally.
If a person needs a pocket, then the pocket is passed to the constructor and added to the class properties . So
сlass Settings {
    public function __construct($db) {
        this->db = $db;
    }
    public function getSiteName() {
        $stmt = $this->db->query("SELECT Value FROM configs WHERE Name = 'SiteName'");
        return $stmt->fetchColumn();
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question