Use smart Architecture in PHP (pick)

Source: Internet
Author: User
Tags bind html header interface object model require return split version
System I wanted to write this article long ago, but there was no time. 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 development for 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 one of 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.





but 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 found that a lot of managers 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).




Basic points of



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 call


These are all I think, in addition to the above mentioned, there must be other points, you can put forward in the forum.





below let us discuss the above points in detail:





1. Database Independent


you are designing, you may not know how much your site is burdened with, so you should remember that you can't bind to a lightweight database, such as MS Access or anything else. 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.





use PHP, for various database function calls are different, you need to use the database for different coding. 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 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.





An example of an HTML class





//Connection Database


require ("database.php");





//often used HTML header/Footer


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





/**


* Controls a bug in the update database


* Checks for data validation and security, and returns true on Success


* returns false on Failure


*


*


*/





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


//Global string, returning 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 the right to update


if (!user_isadmin ()) {


$feedback = "You must is a Admin to Update a Bug";


return false;


}





//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 is 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.





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 of
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 come up with your ideas. 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.