Table of Contents
Updating A Bug
Update Failed!
Updated Bug Successfully
Home php教程 php手册 建立灵巧结构的PHP程序[转]

建立灵巧结构的PHP程序[转]

Jun 21, 2016 am 09:12 AM
html php quot update

程序

建立灵巧结构的PHP程序

时间:2000/10/19 09:06 作者:sharetop 奥索网



很早就想写这篇文章了,但一直没有时间完成它。不是说我来告诉大家如何做,我更希望本文只是做为一个引子,与大家来讨论关于如何建立一个有效地、灵活的网络应用程序。

经过了2-3年的网络应用程序开发工作,我的开发经验变得更加生动了,回过头来看我以前为Geocrawler写的代码,简直不敢相信这是我的。由于GPL的原因,在PHPBuilder中的源码也是良莠不齐的。

最近我做为一个有经验的PHP开发者,一直在帮着写SourceForge,我想这显示出了最终结果的一个范围。好的代码应被分成了多个部分,合适的库及函数调用,清楚的数据库结构,站点的每一个部分与其它部分都是相对独立的。

但是,这仍不是最好的。如果我可以重做,我将更多的关注于HTML层与数据层的分离,通过对象及清楚的函数库实现这一点。



优美的图形

我知道经理们喜欢用优美的图形及图表来描述它们,这将给我们留下最好的印象。用这种隐藏在一个结构后的想法,你可以把你的逻辑与外观分离,这意味着任何一个复杂的程序都可以用"API/Data Access Layer"来表述。

与其你把安全检测、更新的句子等放在HTML层中,不如把它们整体地放在你的API层里。而这个HTML层只含有简单的函数调用和返回的数组、对象或自定的其它什么,以及一些数据库的检索结果的集合等。

如果你这样做了,顶层将是非常的瘦小,你可以方便地创建及维护它。

如下的例子中,这个HTML接口中只有一些API层中的函数的直接调用,一些HTML工具库(它能生成一个弹出框等等),和一些从数据库抽象层中调用的数据库操作方法(你不需要绑定某一个特别的数据库)。



基础

灵活的PHP程序结构最基本的方面有以下几点:

数据库无关性
界面无关性
可移植性
面向对象或至少应由函数库组成

还有其它的?
当然还有一些其它的东西,但我认为那都是太大了,或许你自己能指出它们。


让我们详细地谈谈它们每一条吧。


1、数据库无关性

你从不知道你的站点将会在哪里运行,当然在你创建它时,你希望它变和得很大并且有很高的流量。所以你不想把你自己约束在 MS Access 上面或者其它什么轻便的数据库系统。虽然你不能立刻地插入各种不同的数据库系统,但是你有可能很方便地在它们中间切换。你有一些不同的选择可以把你的数据库调用抽象出来。在PHP中一个奇特的方法是你不得不为每个不同的数据库系统写出不同的代码,因为在PHP中对每一种不同的数据库的访问函数是不同的。为了避开这点,你可以使用一个抽象地数据库访问层,就象PHPLib、下一个版本的PEAR、及我们在SourceForge中描述的那样。


2、界面无关性

一个应用程序是它的技术更重要还是它所运行的站点更重要?我们并不能真正地知道。我从来不相信这一点--HTML是一个标准。特别是对于一个网络应用程序而言,界面发生了改动,意味着我们不得不总是重写。但是如果你的应用程序是很大很复杂的,你就要为你的数据库建立一些其它的接口了,只要你不想在你的站点程序中到处copy&paste你的访问检查等代码。这也意味着,如果你正确地设计了你的应用程序,你可以很容易地改写你的站点让它适应WAP,只要简单地写一个小的WAP界面,并让它调用你的数据库访问对象而已。但若你没有很好地设计你的程序,你把你的HTML版改成WAP版是一个复杂的工程。

我把这个想法也带入了SourceForge中,我们有一个巨大的用户群,为我们发送/接收bugs、任务等。首先,我们指出所有的这些将通过我们的web页面接口,然后,由于Eric Raymond 和其他人给的压力,我们决定用XML来做数据库的外部接口。

幸运的是我们曾在四月已把程序的核心逻辑代码与它的界面分离了。我将试着表达我们是如何做的,希望对你的工作有所帮助。

这个SourceForge的bugs跟踪器和其它的一些工具被分成两个库 - 这个HTML库和数据访问库。这个数据访问库检查输入的值的正确性,处理安全校验,并且当成功/失败时返回TRUE 或 FALSE。

由于简化的原因,这个例子并没有基于一个完善的对象模式,那样我还要解释这个基类和它的一些衍生类等等,我想这个例子将给你一个最普通的想法。


HTML 库的例子


//connect to database
require ("database.php");

//common utils like header/footer HTML
require ("html.php");

//data access library
require ("bug_data.php");

echo site_header("Page Title");

echo "

Updating A Bug


";

if (bug_data_update($field1,$field2,$field3)) {

echo "

Update Failed!

";

} else {

echo "

Updated Bug Successfully

";
//echo the global error string
echo $feedback;
}

echo site_footer();

?>


Data 访问库的例子

/**
*
* controls access to updating a bug in the
* database. Validates data and checks security
* Returns true on success, false on failure
*
*/

function bug_data_update ($field1,$field2,$field3) {
//global string to report back errors
global $feedback;

//$field1 and $field2 are required
if (!$field1 || !$field2) {
$feedback="Field 1 And Field 2 Are Required";
return false;
}

//make sure this user has permission to update
if (!user_isadmin()) {
$feedback="You Must Be An Admin To Update a Bug";
return false;
}

//now let's update the bug

$result=db_query("UPDATE bug ".
"SET field2='$field2',".
"field3='$field3' ".
"WHERE id='$field1'");

//now check your query for success/failure
if (!$result) {
//update failed
return false;
} else {
return true;
}
}

?>


3、可移植性

毫无疑问,你不想让你的代码只能用于一个固定的站点,将来我们可能改变色彩的选择、元素的名称、字体或其它一些什么,这样应设置一个config文件,它被多个页面所包含。更好的观点是你的站点被模块化,你不需要copy&paste任何一个HTML文件,我倾向于把这些放入一个函数,在任何需要的地方调用它们。

同样的方法可用于数据库的密码、数据库连接字串等,这些可以放入一个数据库处理的抽象层中。


4、面向对象/函数化

我们不是用COBOL开发,所以这意味着我们可以把进程分成多个函数的调用。每个调用都是一个自动的行为,有时仅仅是调用一小段其它的函数并返回这个结果。

一个好的例子是在每一个页面校验用户是否登录,你可以用cookie或查询数据库来完成这个功能,但一旦你想改变你的验证系统,你不得不改动每一个页面,其实你应该可以通过改动函数库里一个普通的函数就完成这个变动的。任何时候,你写一段代码,如果它将会被用于多于一个地方,你就要考虑把它放入一个库里了。


其它还有什么?

显然还有很多我没有谈到的事,告诉我你的想法,我将在下一篇文章中来讨论它们。特别地是,如果你写了一个大型的、复杂的应用程序,我想听听你是如何规划它的及你重做时不什么不同的想法。

-------------------
作者:Tim Perdue
译者:sharetop(ycshowtop@21cn.com)
来源:www.phpbuilder.net



Statement of this Website
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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles