I'm new to OOP PHP5 and I'm trying to use the singleton pattern for my Database class. Here is my current code (yes, the getInstance is from the PHP manual):
<?php
final class Database {
private static instance = null;
private $username, $password, $host;
/*
* Prevent an object from being constructed
*/
private __construct() {
}
private __destruct() {
}
/*
* Return the instance of this class
*/
public static function getInstance() {
if(is_null(self::$instance)) {
self::$instance = new Database();
}
return self::$instance;
}
protected function connect($username = "test", $password = "test", $host = "test") {
}
protected function query($query) {
}
}
?>
My questions are:
- How can I restrict this to only one connection which every thing in the file can use?
- How can I make the quiery() function? I'm not quite sure what to return.
This is only suppose to be a basic database class which will allow me to connect and run queries. If anyone has any suggestions and ideas, don't hesitate to write them out.
Thanks!