I do not know why your class has a constructor that invokes another constructor .
If you want to implement a utilitarian class to manage your connections, you can simply make it extend% of% co_ and use the constructor of PDO
to create the instance of the connection.
For security , the connection credentials should not exist defined in constants no matter where. They should be private members of the class, or, for more security, save them in a configuration file that is protected and even isolated for other users with less privileges.
Let's see an example where connection credentials exist as members of the class.
I would propose a class similar to this:
<?php
class DataBase extends PDO
{
private $pdo;
private $dbtype="mysql";
private $host="localhost"; //El host suele ser este, no el nombre de dominio
private $dbname="aynii_db";
private $dbcharset="utf8";
private $usr="";
private $pwd="";
public function __construct()
{
$this->Connect();
}
private function Connect()
{
$host=$this->host;
$dsn = $this->dbtype.":host=".$this->host.";dbname=".$this->dbname.";charset=".$this->dbcharset;
$arrOptions = array(
PDO::ATTR_EMULATE_PREPARES => FALSE,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try
{
$this->pdo = new PDO($dsn, $this->usr, $this->pwd, $arrOptions);
}
catch (PDOException $e)
{
error_log($this->error = $e->getMessage(),0);
}
}
}
?>
Here we pass all the options in the constructor using PDO
, that way we avoid continuing to manage the object once created. We also include the option $arrOptions
in ATTR_EMULATE_PREPARES
to prevent emulated preparations that can be exploited by malicious users to try to emulate prepared queries and inject malicious code.
How to use
The class would be used simply like this:
$db=new Database();
And you would already have a FALSE
an instance of the connection, without having to be transferring credentials.