Build a PHP program with a smart structure

Source: Internet
Author: User
I wanted to write this article for a long time, but I never had time to finish it. I don't mean to tell you how to do it. I hope this article is just a reference to discuss with you how to build an effective and flexible network application. After 2-3 years of network application development, my development experience has become more vivid. looking back, I used to write this article for Geocrawler for a long time, but there is no time to complete it. I don't mean to tell you how to do it. I hope this article is just a reference to discuss with you how to build an effective and flexible network application.

After 2-3 years of network application development, my development experience has become more vivid. looking back, I used to write code for Geocrawler. I can't believe this is mine. Due to GPL, the source code in PHPBuilder is also uneven.

Recently, as an experienced PHP developer, I have been helping to write SourceForge. I think this shows a range of final results. Good code should be divided into multiple parts, suitable library and function call, clear database structure, each part of the site is relatively independent from other parts.

However, this is still not the best. If I can redo it, I will focus more on the separation between the HTML layer and the data layer, and implement this through objects and clear function libraries.



Beautiful images

I know managers like to describe them with beautiful figures and charts, which will give us the best impression. With this idea hidden behind a structure, you can separate your logic from the appearance, which means that any complex program can be expressed by "API/Data Access Layer.

Rather than placing security detection and updated sentences on the HTML layer, you should put them in your API layer as a whole. The HTML layer only contains simple function calls and returned arrays, objects, custom items, and a set of retrieval results of some databases.

If you do this, the top layer will be very small and you can easily create and maintain it.

In the following example, only some functions in the API layer can be directly called in this HTML interface, and some HTML tool libraries (which can generate a pop-up box ), and some database operation methods called from the database abstraction layer (you do not need to bind a special database ).



Basic

The most basic aspects of flexible PHP program structure are as follows:

Database independence
Interface independence
Portability
Object-oriented or at least composed of function libraries

What else?
Of course there are some other things, but I think they are too big. maybe you can point them out by yourself.


Let's talk about each of them in detail.


1. Database independence

You never know where your site will be running. of course, when you create it, you want it to grow bigger and have high traffic. So you don't want to constrain yourself on MS Access or any other lightweight database system. Although you cannot immediately insert different database systems, you may easily switch between them. You have some different options to abstract your database calls. A strange way in PHP is that you have to write different code for each different database system, because the access functions for each different database in PHP are different. To avoid this, you can use an abstract database access layer, just like PHPLib, the next version of PEAR, and what we described in SourceForge.


2. interface independence

Is an application more important to its technology or the site it runs? We cannot really know. I never believe this-HTML is a standard. Especially for a network application, the interface has been changed, which means we have to rewrite it all the time. However, if your application is very complicated, you need to create some other interfaces for your database, as long as you don't want to copy & paste your access check code everywhere in your site program. This also means that if you design your application correctly, you can easily rewrite your website to adapt it to WAP, as long as you simply write a small WAP interface, and let it call your database access object. However, if you do not have a good design for your program, it is a complicated project to change your HTML version to WAP version.

I also brought this idea into SourceForge. we have a huge user group that sends/receives buckets and tasks for us. First, we point out that all these will be implemented through our web page interface. then, due to the pressure from Eric Raymond and others, we decided to use XML as the external interface of the database.

Fortunately, in April, we separated the core logic code of the program from its interface. I will try to express how we do it and hope it will help your work.

This SourceForge bugs tracker and some other tools are divided into two libraries-this HTML library and data access library. This data access database checks the correctness of the input values, handles security verification, and returns TRUE or FALSE if the input values are successful or fail.

For the sake of simplification, this example is not based on a perfect object mode, so I need to explain this base class and some of its struct classes, etc, I think this example will give you the most common idea.


Example of an HTML Library


// 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 database access example

/**
*
* Controls access to updating a bug in
* 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. portability

Without a doubt, you don't want your code to be used only for a fixed site. in the future, we may change the color selection, element name, font, or something else, in this case, you should set a config file that is contained by multiple pages. A better idea is that your site is modularized and you don't need to copy & paste any HTML file. I prefer to put these into a function and call them wherever needed.

The same method can be used for database passwords, database connection strings, and so on. these can be placed in an abstraction layer for database processing.


4. object-oriented/functional

We do not use COBOL for development, so this means that we can divide the process into multiple function calls. Each call is an automatic action. sometimes it is only a short call to other functions and returns this result.

A good example is to check whether a user is logged on every page. you can use cookies or query databases to complete this function. However, once you want to change your verification system, you have to change every page, in fact, you should be able to complete this change by modifying a common function in the function library. At 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.

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.