Use smart Architecture in PHP

Source: Internet
Author: User
Tags object model

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 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 through the object or function library.

I found that a lot of the governing people like to use the form of graphs to express their ideas, here I also provide 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 here:

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 "

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.