Use smart Architecture in PHP

Source: Internet
Author: User
Tags bind object model require split

I've been wanting to write this article a long time ago, but I've never had a moment. I'm not here to tell you what to do, I hope it can testing and discuss how to develop a good, scalable Web application.

I have been engaged in the development of 2-3 years, looking back at the beginning of the program, really a bit do not believe that I wrote, and now my web development skills have been greatly improved, such as SourceForge http://sourceforge.net/is my more mature works, The code is divided into various classes and functions. The structure of the database is also very clear. Different parts of the site are separate from the rest.

However, this site is not perfect. If I had to write again, I would make the HTML layer more distinct from the database layer by the way the object or function library.

I have found that many managers like to express their ideas in the form of graphs, and here I offer one. The idea of this system is to separate your logic from the surface, which means that anything that is complicated will be delegated to the "api/data Access Layer".

For security checks, updates and other code, you'd better not put them in the HTML layer, you should put these theoretical code into the API layer. The HTML layer will only make simple function calls and return the array, object, or my favorite database result set.

In this diagram, the HTML interface either invokes the API layer directly, or invokes an HTML ToolPak (such as a pop-up window), and those libraries call the database through a Database abstraction layer (so you don't have to bind to some kind of database).

The basic points
For a smart system, there are the following basic points:
1. Database Independent
2. Presentation Layer Independent
3. Easy to modify
4. Object-oriented or at least split into function library calls
These are all I think, in addition to the above mentioned, there must be other points, you can put forward in the forum.

Let us discuss the above points in detail below:

1. Database Independent
When you design, you may not know how much the burden of your site, you should remember that you can not bind to a lightweight database, such as MS Access or other. So you should consider scalability, if you change the database, you do not have to make too big changes, and even do not make any changes, this is the ideal.

When using PHP, the function calls for various databases are different, and you need to encode different encodings for the databases you use. To change this, you can use a database abstraction layer, such as a phplib or a library developed by someone else.

2. Presentation Layer Independent
If you're going to develop a really big, complex application, you'll need to start thinking about database interface issues, so you can do a lot less copying and pasting work. For example, you need to make your site a WAP feature so that mobile phone users can access it. If your application is well designed, you just need to write a lightweight WAP presentation layer to call all your database access objects, but if your application is poorly designed, you may need to write a new one so that you need to maintain both an HTML version and a WAP version.

For example, when developing a SourceForge site, we have a large number of users submitting their bugs and tasks. At first we designed it all through the Web interface. Later, under pressure from some people, we decided to use an XML interface to present the database. We succeeded in separating the core logic of the site from the presentation layer. Bug tracking and other tools on SourceForge now use two different libraries--html library classes and database classes. The data class is responsible for detecting whether the value entered is valid and handling security detection, and the presentation layer simply returns TRUE or FALSE based on success/failure. For simplicity, this example will not be based on a perfect object model when I have to explain how the base class and other objects extend these base classes. But I think this example will help you build some ideas.

Examples of HTML classes

Connecting to a database
Require ("database.php");

Commonly used HTML headers/footers
Require ("html.php");

Data Access Library class
Require ("bug_data.php");

Echo Site_header ("Page Title");

echo "

Updating A Bug


";

if (Bug_data_update ($field 1, $field 2, $field 3)) {

echo "

Update failed!

";

} else {

echo "

Updated Bug successfully

";
Display Global error string
Echo $feedback;
}

Echo Site_footer ();

?>

Example Data Access Lib

/**
* Control a bug in the update database
* Check for data validation and security, and return True when successful.
* Return False when failed
*
*
*/

function Bug_data_update ($field 1, $field 2, $field 3) {
Global string, returning an error
Global $feedback;

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

Confirm that the user has permission to update
if (!user_isadmin ()) {
$feedback = "You must is a Admin to Update a Bug";
return false;
}

You can now update the bug

$result =db_query ("UPDATE bug").
"SET field2= ' $field 2 ',".
"Field3= ' $field 3 '".
"WHERE id= ' $field 1 '");

Now check that your statement was executed successfully
if (! $result) {
Update failed
return false;
} else {
return true;
}
}

?>


3. Easy to modify
You certainly don't use absolute URLs for the entire application, but I also want to go further, color choices, element names, fonts, and other possible options are best not absolute, they should be set in a configuration file and included in each page. The style of the site should also be independent-so that you don't have to copy and paste on every page, I usually put the HTML in a function, and then I can call it when I need it.

It is also placed in the database abstraction layer for database passwords, database connections, and so on.

4. Object-oriented/function
We can split process processing into different function calls. Every call does one thing, and sometimes it just needs to call another function and return the result.

A good example is to check whether a user is logged in on each page. If you don't use objects or functions, you have to make changes on each page when your authentication system changes, instead of just changing the call to a function in the library. Before you write a piece of code, think about it, and if it's going to be used more than once in the site, you have to move it to the library to implement it. Do you have anything to add?
Sure I still have a few places to think about, so please present your thoughts. In particular, you have written a very large and complex application, and I would like to know what system you will build and what changes will be made if you write it again.



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.