I'm new to OOP programming and i'm trying to set up a private class db, for database connection:
My class:
class db
{
private $_db;
private function db()
{
$this->db = new mysqli('localhost','x','x','x');
$this->db->set_charset('utf');
}
}
In another file I have the following code:
$db = new db();
$sql = "SELECT news, DATE_FORMAT(date, '%D %b') AS date FROM news ORDER
BY DATE_FORMAT(date, '%Y, %m, %d') DESC LIMIT 3";
$r = $db->db->query($sql);
while($row=$r->fetch_assoc())
{
echo '<b>'. $row['date'] . '</b></br> ' . $row['news'] .'</br>
</br>';
}
It works grand for public but not for private.
I was reading that private methods can only be accessed within the class but can't really understand how to do it. Can anyone please give me a hint?
Regards Jack