PHP Development App Interface (VII)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.