Software architecture uses smart architecture in PHP

Source: Internet
Author: User
Tags html header
I wanted to write this article a long time ago, but I never had the time. This is not to tell you what to do, I hope it can be ofspecialty and discuss how to develop a good, extensible Web application.
I have been in the development for 2-3 years, looking back at the beginning of the process, I really do not believe it is written by myself, now my web development skills have been greatly improved, such as SourceForge (http://sourceforge.net/) is my more mature a work, The code is divided into classes and functions. The structure of the database is also very clear. The different parts of the site are separate from the rest.
However, this site is not perfect. If I had to write it again, I would make the HTML layer more clearly distinguishable from the database layer, either through an object or a library of functions.
I find a lot of managers like to use charts 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 complicated will be relegated to the api/data access layer.
For security checks, updates and other code, you'd better not put in the HTML layer, you should put these theoretical code to the API layer. The HTML layer will simply make a simple function call and return an array, an object, or my favorite database result set.
In this diagram, the HTML interface either calls the API layer directly, or calls an HTML tool library (such as a popup window), and those libraries can invoke the database through a Database abstraction layer (so you don't have to bind to a database).
The basic point
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 a function library call
This is what I think, in addition to the above mentioned, there must be other points, you can put forward in the forum.
Let's discuss the above points in detail below:
1. Database Independent
When you are designing, you may not know how much the burden of your site is, so you should remember that you cannot bind to a lightweight database, such as MS Access or anything else. Therefore, you should consider the extensibility, if you change the database, you do not have to make too much change, or even do not make any changes, this is ideal.
When using PHP, the function calls for various databases are different, and you need to encode them differently for the database you are using. To change this, you can use a database abstraction layer, such as a library developed by Phplib or others.
2. Presentation Layer Independent
If you want to develop a really big, complex application, you have to start thinking about database interface problems, so you can do a lot less copy and paste work. For example, you need to have a WAP feature on your site so that users of your mobile phone can access it. If your application is well designed, you just need to write a lightweight WAP presentation layer to invoke all your database access objects, but if your application is poorly designed, you might need to write one again, so you need to maintain an HTML version and a WAP version at the same time.
For example, when developing sourceforge sites, we have a large number of users who want to submit their bugs and tasks. At the beginning we designed it all through the Web interface. Later, under pressure from some people, we decided to use the 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 input value is valid and handling security detection, whereas the presentation layer returns TRUE or FALSE based on success/failure only. To simplify, when I have to explain how base classes and other objects extend these base classes, this example will not be based on a perfect object model. But I think this example can help you build some concepts.
Examples of HTML classes
Connecting to a database
Require ("database.php");
Commonly used HTML header/Footer
Require ("html.php");
Data Access Library classes
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
* Perform data validation and security checks, and return true on success
* Return false upon failure
*
*
*/
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;
}
The bug can now be updated
$result =db_query ("UPDATE bug".
"SET field2= ' $field 2 ',".
"Field3= ' $field 3 '".
"WHERE");
Now check that your statement is executed successfully
if (! $result) {
Update failed
return false;
} else {
return true;
}
}
?>
3. Easy to modify
You certainly don't use absolute URLs throughout the app, but I'm also asking for further, the choice of colors, the names of the elements, the 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 you do not have to copy and paste every page of the work, I usually put these HTML in a function, and then can be called when needed.
For database passwords, database connections, and so on, it is also placed in the database abstraction layer.
4. Object-oriented/function
We can split the process processing into different function calls. Each call does one thing, and sometimes it just needs to call another function and return the result.
A good example is checking each page to see if a user is logged in. If you do not use objects or functions, you will have to make changes on each page when your authentication system changes, rather than just changing the invocation of 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 else to add?
I'm sure there are some places I didn't think of, so please come up with your ideas. In particular, you have written a very large and complex application, and I would like to know what kind of system you will build and what changes you will make if you want to write it again.

The above describes the software architecture in PHP using a smart architecture, including the software architecture aspects, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.