search
Homephp教程php手册PHP – 架構設計 Data Access Layer 篇

 

首先,大概陈述一下架构的关联,如下所述:

首先会先设计 标准 DAL class (STDAL),放置 getData, delete, update 等 标准常见的功能函数
在来设计程式会用到的各种 DAL ,基本上每一个 Table 都需要有一个 DAL 来实现,后面根据 table 应用、画面呈现等需求,也可以一个 table 有多个 DAL ,这各观念类似 View 的概念。
根据 商业逻辑的操作,制作对应的 BLL,像是insert、update 前的资料检查,这部分会根据商务应用的不同而不同,所以下面不做示范。
在来要有一个 DAL 产生工厂(DALFactory),专门用来协助建立 DAL 的实体,因为 DAL 程式档案,可能放在另一台主机,或是 不同目录位置中等等因素,为简化开发人员的负担,所以 DAL 建立方式,统一封装在 DALFactory 里面
接下来,依照上述建了下述程式:

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 <?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 ?>

 

下面 DAL 产生工厂,有运用我在 PHP – 类别初探 中所讲的技巧,有兴趣可在去看下。
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 ?>

 

上述就已经完成 Data Access Layer 的制作,接下来 我门测试一下,是否正常运作。

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 ?>

測試結果,如下所示:
STUser init STDAL
STUser get DB connection
select * from STUser
STDoc init STDAL
STUser get DB connection
select * from STDoc



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software