I just tried a PDO wrapper from https://phpdelusions.net/pdo/pdo_wrapper.
First the PDO Wrapper
class MyPDO
{
protected static $instance;
protected $pdo;
public function __construct() {
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
PDO::ATTR_EMULATE_PREPARES => FALSE,
);
$dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset='.DB_CHAR;
$this->pdo = new PDO($dsn, DB_USER, DB_PASS, $opt);
}
// a classical static method to make it universally available
public static function instance()
{
if (self::$instance === null)
{
self::$instance = new self;
}
return self::$instance;
}
// a proxy to native PDO methods
public function __call($method, $args)
{
return call_user_func_array(array($this->pdo, $method), $args);
}
// a helper function to run prepared statements smoothly
public function run($sql, $args = [])
{
if (!$args)
{
return $this->query($sql);
}
$stmt = $this->pdo->prepare($sql);
$stmt->execute($args);
return $stmt;
}
}
and then the user class
class User
{
/* @var MyPDO */
protected $db;
protected $data;
public function __construct()
{
$this->db = MyPDO::instance();
}
public function find($id)
{
$this->data = $this->db->run("SELECT * FROM user_account WHERE id = ?", [$id])->fetch();
}
}
and then instantiate class user
$user = new User();
$a = $user->find(3);
var_dump($a);
There is a record associated with ID = 3 but the result I get is NULL, why?