Php-architecture design data access layer

Source: Internet
Author: User

In the preface to the multi-level architecture design, I have talked about a bunch of articles to illustrate the later architecture design. In this way, we can look at the significance of these articles from a broader perspective.

First, describe the relationship between architectures as follows:

First, standard Dal class (stdal) will be designed, and standard common functions such as getdata, delete, and update will be placed.
Basically, each table needs a Dal to implement the various Dal used in the design program. Later, a table can have multiple Dal according to the needs of the table application and screen presentation, this concept is similar to the concept of view.
According to the business logic, make the corresponding Bll, such as the data check before insert and update. This section varies with the business application, so we will not demonstrate it below.
There is a dal Production Plant (dalfactory) to assist in the creation of the Dal entity, because the Dal program file may be placed on another host, or in different directory locations, and so on, to simplify the burden on developers, the Dal establishment method is encapsulated in dalfactory.
Next, the following program is created based on the above:

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      10     public function getData()11     {12         print "select * from ".$this->TableName."<br>";13     }14      15     public function setDB($db)16     {17         echo $db."<br>";18     }19 }20 ?>

 

Stuser. php

1 <?php2 class DAL_STUser extends STDAL3 {4     public function __construct() {5         $this->TableName = "STUser";6         parent::__construct();7     }8 }9 ?>

 

Stdoc. php

1 <?php2 class DAL_STDoc extends STDAL3 {4     public function __construct() {5         $this->TableName = "STDoc";6         parent::__construct();7     }8 }9 ?>

 

The following Dal production factory is applicable to the skills I have mentioned in the PHP-category preliminary study. If you are interested, please check them out.
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"; 10         }11         $class = "DAL_$prgName";12         $obj = new $class();13         $obj->setDB(self::$db);14         return $obj;15     }16 }17 ?>

 

The above steps have completed the creation of the data access layer. Next, let's test whether it works properly.

Test. php

1 <?php2 $prgName = "STUser";3 $obj = DALFactory::getInstance($prgName);4 $obj->getData();5  6 $prgName = "STDoc";7 $obj = DALFactory::getInstance($prgName);8 $obj->getData();9 ?>

The results are as follows:
Stuser init stdal
Stuser get dB connection
Select * From stuser
Stdoc init stdal
Stuser get dB connection
Select * From stdoc

A small shard

Next PHP-easyui DataGrid Data Retrieval Method

 

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.