Create a smart structure of the PHP program [turn]
Source: Internet
Author: User
Program to build a smart structure of the PHP program
Time: 2000/10/19 09:06 Author: sharetop Osso net
I have long wanted to write this article, but I have no time to finish it. Not that I'm here to tell you how to do it, but I hope this article is just a primer on how to build an effective, flexible network application.
After 2-3 years of Web application development work, my development experience became more vivid, looking back at the code I had written for Geocrawler, I couldn't believe it was mine. Because of the GPL, the source code in the Phpbuilder is also a mixed problem.
I've been working as an experienced PHP developer recently to help write SourceForge, and I think it shows a range of the final results. Good code should be divided into several parts, appropriate library and function calls, clear database structure, each part of the site and other parts are relatively independent.
However, this is still not the best. If I can redo it, I'll focus more on separating the HTML layer from the data layer, and implementing this through objects and a clear library of functions.
Beautiful graphics
I know managers like to use beautiful graphs and graphs to describe them, which will give us the best impression. With this idea of hiding behind a structure, you can separate your logic from the look, which means that any complex program can be expressed with "Api/data Access Layer".
Instead of putting security checks and updated sentences in the HTML layer, you might as well put them in your API layer as a whole. This HTML layer contains only simple function calls and returned arrays, objects, or whatever else, and a collection of retrieval results for some databases.
If you do this, the top floor will be very skinny and you can easily create and maintain it.
As in the example below, this HTML interface has only a few direct calls to functions in the API layer, some HTML ToolPak (which generates a pop-up box, and so on), and some database manipulation methods called from the database abstraction layer (you don't need to bind a particular database).
Basis
The most basic aspects of a flexible PHP program structure are the following:
Database Independence
Interface Independence
Portability
Object-oriented or at least composed of function libraries
Is there anything else?
There are other things, of course, but I think it's too big, maybe you can point them out.
Let's talk about every one of them in detail.
1, database-independent nature
You never know where your site will run, but when you create it, you want it to be large and have high traffic. So you don't want to constrain yourself to MS Access or any other lightweight database system. Although you can't plug in all the different database systems right away, you might be able to easily switch between them. You have a number of different options to abstract your database calls. A peculiar way to do this in PHP is that you have to write different code for each of the different database systems, because the access functions for each of the different databases are different in PHP. To avoid this, you can use an abstract database access layer, like Phplib, the next version of Pear, and what we described in SourceForge.
2, the interface of Independent
is an application more important than its technology or is it running a site? We don't really know. I never believed it.--html is a standard. Especially for a Web application, the interface changes, meaning we have to always rewrite. But if your application is big and complex, you have to build some other interface for your database, as long as you don't want to copy&paste your access checks in your site program. This also means that if you design your application correctly, you can easily rewrite your site to allow it to adapt to WAP, simply write a small WAP interface and have it call your database access object. But if you don't design your program well, it's a complex project to change your HTML version to a WAP version.
I also brought this idea into the SourceForge, we have a huge user base for us to send/receive bugs, tasks, etc. First, we point out that all of this will go through our web page interface, and then, thanks to the pressure from Eric Raymond and others, we decided to use XML to do the external interface of the database.
Luckily we had separated the core logic code of the program from its interface in April. I will try to express how we do it and hope to help you with your work.
This SourceForge bugs tracker and other tools are divided into two libraries-the HTML library and the data Access library. This data access library checks the correctness of the input values, handles the security checksum, and returns TRUE or FALSE when successful/unsuccessful.
For reasons of simplification, this example is not based on a perfect object pattern, so I have to explain the base class and some of its derivative classes, and so on, I think this example will give you the most common idea.
Examples of HTML libraries
<?php
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 "<P> ";
if (Bug_data_update ($field 1, $field 2, $field 3)) {
echo "
} else {
echo "echo the global error string
Echo $feedback;
}
Echo Site_footer ();
?>
Examples of Data access libraries
<?php
/**
*
* Controls access to updating a bugs in the
* Database. Validates data and checks security
* Returns true on success, false on failure
*
*/
function Bug_data_update ($field 1, $field 2, $field 3) {
Global String to errors
Global $feedback;
$field 1 and $field 2 are required
if (! $field 1 | |! $field 2) {
$feedback = "Field 1 and Field 2 Are Required";
return false;
}
Make sure this user has permission to update
if (!user_isadmin ()) {
$feedback = "You must is a Admin to Update a Bug";
return false;
}
Now check your query for success/failure
if (! $result) {
Update failed
return false;
} else {
return true;
}
}
?>
3. Portability
There is no doubt that you do not want your code to be used only for a fixed site, in the future we may change color selection, element name, font, or something else, so you should set a config file, it is included in multiple pages. The better view is that your site is modular and you don't need to copy&paste any HTML files, and I tend to put these in a function to call them wherever they are needed.
The same method can be used for database passwords, database connection strings, and so on, which can be placed in a database processing abstraction layer.
4. Object-oriented/functional
We're not developing in COBOL, so that means we can divide the process into calls to multiple functions. Each invocation is an automatic behavior, sometimes simply by calling a small segment of other functions and returning the result.
A good example is to check whether a user is logged on every page, you can use cookies or query database to do this function, but once you want to change your verification system, you have to modify every page, in fact, you should be able to change the function library in a normal function to complete this change. Any time you write a piece of code, if it will be used in more than one place, you should consider putting it into a library.
What else is there?
Obviously there are a lot of things I haven't talked about, tell me what you think, and I'll discuss them in the next article. In particular, if you write a large, complex application, I'd like to hear how you plan it and what you don't think is different when you redo it.
-------------------
Author: Tim Perdue
Translator: Sharetop (ycshowtop@21cn.com)
Source: Www.phpbuilder.net
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