Reputation: 1864
i created a small PDO class and spent hours in debugging it and could not find a small typo which was causing every thing to fail. To demonstrate below is the buggy code.
class MyPDO extends PDO
{
private static $instance = null;
function __construct(){
try{
parent::__construct("mysql:host=localhost;port=3306;dbname=blog", "root", "");
parent::setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}catch(PDOException $e){
echo 'Exception in constructor'.print_r($e->trace(),true);
}
}
static public function getDB(){
if(self::$instance == null){
self:$instance = new MyPDO();
}
return self::$instance;
}
function selectAll($sql){
$stmt = self::$instance->prepare($sql);
$stmt->execute(array(":cat_id"=>1));
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
What i want to confirm that if any one has already seen it or is it a bug or it could be used for some thing else i have no knowledge of.
There is a problem with the following line i wrote.
self:$instance = new MyPDO();
it should be the scope resolution operator with double colon i.e.
self::$instance = new MyPDO();
To my surprise no warning or error is generated with a single colon .. If any one knows about this please share.
Upvotes: 1
Views: 126