PHP design pattern series-Data Access Object pattern

Source: Internet
Author: User
Data Access Object mode describes how to create objects that access the data source transparently. Design a BaseDao base class in the scenario to implement basic query, insert, and update operations in the database. in actual use, inherit BaseDao, and you can directly call the base class... syntaxHighlighter. all (

Data Access Object mode
The Data Access Object mode describes how to create objects that access the data source transparently.
Scenario Design
Design a BaseDao base class to implement the basic query, insert, and update methods for database operations.
In actual use, you can directly call the base-class database operation method by inheriting BaseDao.
Code: BaseDao database operation base class
[Php]
// Data Access Object mode
 
// Remove the database access layer as a public access interface, which is convenient for users. it is a common design mode in php.
 
Class BaseDao {
Private $ db;

Public function _ construct ($ config ){
$ This-> db = mysql_connect ($ config ['user'], $ config ['pass'], $ config ['host']);
Mysql_select_db ($ config ['database'], $ this-> db );
}

Public function query ($ SQL ){
Return mysql_query ($ SQL, $ this-> db );
}
}

Code: data operations on UserDao user data tables, inherited from BaseDao
[Php] www.2cto.com
Include ("UserDao. php ");
Class UserDao extends BaseDao {
Public function addUser (){
$ SQL = "INSERT INTO user (username) VALUES ('initphp ')";
Return $ this-> query ($ SQL );
}
}
 
$ UserDao = new UserDao;
$ UserDao-> addUser ();


Author: initphp

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.