I am trying to get this to work but it keeps giving me an error about $this not being in an object context for the line:
echo $this->db->query("SELECT * FROM enquiries") ;
Where am I going wrong? 🙁
class CoreModel {
protected $db ;
function __construct()
{
try{
$this->db = new PDO("mysql:host=localhost;dbname=database", "user", "pass") ;
$this->db->exec('set names utf8') ;
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$this->db->setAttribute(PDO::ATTR_PERSISTENT, TRUE) ;
}
catch(PDOEXCEPTION $e)
{
echo $db->errorCode ;
die() ;
}
}
function test()
{
echo $this->db->query("SELECT * FROM enquiries") ;
}
function __destruct()
{
$this->db = NULL ;
}
}
echo CoreModel::test() ;
Thanks, I knew I was missing something obvious!
You have no
$thisif you don’t create an instance of your CoreModel class. You can’t call this function statically.Create an instance using the
newoperator :