Answer the question
In order to leave comments, you need to log in
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;
}
}
function classLoad($Class) {
include $_SERVER["DOCUMENT_ROOT"]."/".mb_strtolower($Class).".php";
}
spl_autoload_register("classLoad");
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"];
}
}
}
require_once $_SERVER["DOCUMENT_ROOT"]."/autoload.php";
$TEST = new App\Settings();
$TEST->getSiteName();
Answer the question
In order to leave comments, you need to log in
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;
}
}
с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 questionAsk a Question
731 491 924 answers to any question