Class Mysqlconn {
Defines a private static property that is used to hold an instantiated object
private static $dbcon;
Defines a private static property that is used to connect to a database
private static $conn;
Define a private destructor to connect to the database
Private Function __construct ()
{
Self:: $conn = Mysqli_connect ($host, $username, $password, $dbname) or Die (' link failed: '. Mysqli_error (). Mysqli_errno ());
}
To define a private clone method
Private Function __clone ()
{
Todo:implement __clone () method.
}
Common static method used to return class instances
public static function getinstance () {
if (!self:: $dbcon) {
Self:: $dbcon = new self ();
}
Return self:: $dbcon;
}
The various functions of MySQL can be defined later
Public Function Search ($sql) {
$result = Mysqli_query (self:: $conn, $sql);
Return Mysqli_fetch_assoc ($result);
}
}
Calling methods
$conn = Mysqlconn::getinstance ();
$sql = "SELECT * FROM ' table_name '";
$rows = $conn->search ($sql);
Echo ' <pre> ';
Var_dump ($rows);
PHP Singleton mode package MySQL class