PHP Object-oriented Advanced design pattern: Data Access Object mode

Source: Internet
Author: User
Tags php and mysql
What is the data Access object pattern?

The data Access object design pattern describes how to create an object that provides transparent access to any data.

Application problem and solution of data access object pattern

For those who have also learned PHP and MySQL, the data Access object design pattern is a new and exciting concept. The purpose of this design pattern is to solve the following two specific problems: duplication and abstraction of the data source.

We should create a data access object design pattern object. This data access object encapsulates the intelligent way to create a SQL call, reduce the complexity and repetition of instance creation, and update the process by writing that the consumer of the object does not know what table structure is actually used and the database engine. In addition, the methods applied by this object should use logical parameters, and the creation of SQL statements should be handled.

The additional benefit of the data access object pattern is to provide a database abstraction layer. Now, the primary processing code for your application no longer needs to take into account the database engine or table relationships. Public methods that call this object return any data type, regardless of the type required by the intrinsic SQL.

A good way to manage the simplicity of data Access object classes is to create a parent-child relationship. First, create a basic parent object. This object should be responsible for database connections, executing queries in an abstract manner, and communicating with child objects. When using the data Access object design pattern, it is a good idea to start associating a one-to-one relationship subclass with a table in the database. These subclasses have essential information, such as table names and primary keys. In addition, subclasses may contain specific public methods that execute queries of the parent class in a way that is meaningful only to the child class. For example, a subclass named UserAddress might contain a getaddreddesbyzip () method. Putting the method into the parent DAO class is illogical and destroys the abstraction that the parent wants to implement.

When working with entities that reference specific database information, it is a best practice to create a data access object.

<?php////Data Access Object mode  //separate the database access layer as a common access interface, convenient for users to open, is a common design pattern 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: Userdao Data manipulation of user data tables, inheriting Basedao

<?php 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 ();

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.