First, I would like to state the structure of the association, as described below:
First of all, we will design standard DAL class (Stdal), put GetData, delete, update and other standard common function functions
In the design program will use a variety of DAL, basically each table needs to have a DAL to implement, the following according to the table application, screen rendering and other requirements, can also be a table with multiple dal, the concept of similar view.
According to the operation of business logic, make corresponding BLL, such as INSERT, update before the data inspection, this part will be based on the different business applications, so do not do the following demonstration.
In order to have a DAL production plant (dalfactory), which is designed to assist in the creation of the Dal entity, because the DAL program files may be placed on another host, or in different directory locations, and so on, to simplify the burden on developers, the DAL is built in a unified package in Dalfactory inside
Next, the following procedures have been built:
stdal.php
1 <?php
2 class Stdal
3 {
4 public $TableName;
5
6 Public function __construct () {
7 echo $this->tablename. "Init stdal<br>";
8 }
9
public function GetData () { print ' select * from '. $this-> TableName. " <br> "; the public function Setdb ($db) is $db. <br> ";
?>}
stuser.php
1 <?php
2 class Dal_stuser extends Stdal
3 {
4 public function __construct () {
5 $this-> TableName = "Stuser";
6 parent::__construct ();
7 }
8}
9?>
stdoc.php
1 <?php
2 class Dal_stdoc extends Stdal
3 {
4 public function __construct () {
5 $this- >tablename = "Stdoc";
6 parent::__construct ();
7 }
8}
9?>
Below the DAL production plant, there is the use of my php– category in the discussion of the skills, I am interested to see.
dalfactory.php
1 <?php
2 class Dalfactory
3 {
4 private static $db;
5
6 Public static function getinstance ($prgName) {
7
8 if (!self:: $db) {
9 self:: $db = $prgName. "Get DB connection";
One $class = "Dal_$prgname"; $obj = new $class (); $obj->setdb (self:: $db);
return $obj;
?>}
The above has completed the production of Data Access Layer, and then I test the door, whether normal operation.
test.php
1 <?php
2 $prgName = "Stuser";
3 $obj = dalfactory::getinstance ($prgName);
4 $obj->getdata ();
5
6 $prgName = "Stdoc";
7 $obj = dalfactory::getinstance ($prgName);
8 $obj->getdata ();
9?>
Test results, as follows:
Stuser Init Stdal
Stuser Get DB Connection
SELECT * FROM Stuser
Stdoc Init Stdal
Stuser Get DB Connection
SELECT * FROM Stdoc