App Interface Instances
Single-instance mode connection database
The development of homepage interface and the demo of client app
App Version Upgrade interface
Interface for app error logs
Single-instance mode connection database
Learning Essentials:
Mastering the design of single case pattern
PHP Connection Database
Singleton mode: A class can have only one instance;
<?php
Class db{
Puclic function __construct () {
}
}
$DB =new db ();
$db 1=new Db ();
A common class can produce multiple instances
The three principles of the singleton pattern:
1. The constructor needs to be marked as non-public (prevent external use of the new operator), and the Singleton class cannot be instantiated in other classes, but only by itself.
2. A static member variable $_instance that holds an instance of the class;
3. Have a public static method that accesses this instance.
Class Ndb () {
Private Function __construct () {
}
}
So you can't instantiate the class.
<?php
class Ndb () {
Static private $_instance;//specification
static private $_connectsource;
Private $_dbconfig = Array (
' host ' = ' 127.0.0.1 ',
' user ' = ',
' pass ' = ',
' database ' = ',
)
Private Function __construct () {
}
Public static function getinstance () {
if (! ( Self:$_instance instanceof Self)) {
self::$_instance = new self ();
}
return self::$_instance;
}
Public function Connect () {
if (!self::$_connectsource) {
Self::$_connectsource=mysqli_connect ();
if (!self::$_connectsource) {
throw new Exception ("Mysql_connet_error". Mysql_error ());
//Die ("Mysql_connet_error". Mysql_error ());
}
mysql_query (' Set names UTF8 ');
}
return Self::$_connectsource
}
}
?>
$ndb =ndb::getinstance ();
PHP Development App Interface (vii)