The Codes:
class mysql_db{
private $conn;
private function connect(){
if(isset($this->$conn)){
$this->$conn = new mysqli("localhost", "php_user", "php_pass", "db");
if($this->$conn->connect_error)
die("Connection failed: " . $this->$conn->connect_error);
}
}
private function disconnect(){
$this->$conn->close();
unset($this->$conn);
}
public function getContent(){
$this->connect();
$stmt = $this->$conn->prepare("SELECT * FROM content");
if($stmt->execute()){
$stmt->bind_result($id, $type, $title, $text, $inserted);
while($stmt->fetch()){
printf("%s %s %s %s %s\n",$id, $type, $title, $text, $inserted);
}
}
disconnect();
}
}
$db = new mysql_db();
$db->getContent();
Result:
Notice: Undefined variable: conn in db.php on line 5
Notice: Undefined variable: conn in db.php on line 18
Fatal error: Cannot access empty property in db.php on line 18
The Question:
Why this happen, and how to fix it?
The Target:
Creating a connection class while limiting it user to use only PUBLIC function. Keeping all access to database inside it own class.
EDIT:
Solution: It was only $ mistake.