This article mainly and everyone to share PHP single example design mode connection database in detail, mainly in the form of code and we share, hope to help everyone.
<?php /** *php Object-oriented: single-state design mode (connection database) */ class db{ private static $obj =null; Declare some information about a private database object //database connection private function __construct () { echo "connection to database succeeded"; } Returns the database connection object (static method) static function getinstance () { if (Is_null (self:: $obj)) { //database object does not exist when self :: $obj =new self (); Instantiation } return self:: $obj; Return database Object } } $db =db::getinstance (); Database Object $db->query ("select * from ' Student '");
Related recommendations:
This paper introduces three kinds of design patterns commonly used in PHP: single-case design mode, factory design pattern and observer design pattern.