Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use smart architecture in PHP

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use the smart architecture in PHP, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

How to use smart architecture in PHP

For security checks, updates and other code, you'd better not put it in the HTML layer, you should put the theoretical code into the API layer. The HTML layer will only make simple function calls and return arrays, objects, or my favorite database result set.

In this figure, the HTML interface either calls the API layer directly, or invokes a HTML tool library (for example, generating pop-up windows, etc.), and those libraries can call the database through a database abstraction layer (so you don't have to bind to a database).

Basic essentials

For a smart system, there are the following basic points:

one. Database independence

two. Presentation layer independent

three. Easy to modify

four. Object-oriented or at least split into function libraries to call

These are all of my mind, in addition to the above mentioned, there must be other key points, you can put forward in the forum.

Let's discuss the above points in detail:

one. Database independence

When you design, you may not know the burden of your site, so you should keep in mind that you can't bind to a lightweight database, such as MSAccess or anything else. So you should consider extensibility, if you change the database, you don't have to change much, or even make any changes, which is ideal.

When using PHP, the function calls for different databases are different, and you need to code differently for the database you are using. To change this, you can use a database abstraction layer, such as libraries developed by PHPLib or others.

two. Presentation layer independent

If you want to develop a really large and complex application, you have to start thinking about database interfaces, so you can do a lot less copy and paste work. For example, you need to make your site WAP-enabled so that mobile phone users can access it. If your application is well designed, you only need to write a portable WAP presentation layer to call all your database access objects, but if your application architecture is not well designed, you may need to write a new one, so you need to maintain both a HTML version and a WAP version.

For example, when developing a SourceForge site, we have a large number of users to submit their bug and tasks, and so on. At the beginning, we designed it all through the web interface. Later, under the pressure of some people, we decided to use the XML interface to present the database. We successfully separated the core logic of the site from the presentation layer. Today's bug tracking and other tools on SourceForge use two different libraries-the HTML library class and the database class. The data class is responsible for checking whether the entered value is valid and handles security checks, while 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 base classes and other objects extend these base classes. But I think this example can help you build some concepts.

Example of the HTML class

/ / Connect to the database

Require ("database.php")

/ / commonly used HTML header / footer

Require ("html.php")

/ / data access library class

Require ("bug_data.php")

Echosite_header ("PageTitle")

Echo "

UpdatingABug

"

If (bug_data_update ($field1,$field2,$field3)) {

Echo "

UpdateFailed!

"

} else {

Echo "

UpdatedBugSuccessfully

"

/ / display global error string

Echo$feedback

}

Echosite_footer ()

? >

ExampleDataAccessLib

/ * *

* Control and update a bug in the database

* check the validity and security of the data and return true if successful

* return false on failure

*

*

* /

Functionbug_data_update ($field1,$field2,$field3) {

/ / Global string, error returned

Global$feedback

/ / $field1and$field2arerequired

If (! $field1 | |! $field2) {

$feedback= "Field1AndField2AreRequired"

Returnfalse

}

/ / confirm that the user has the right to update

If (! user_isadmin ()) {

$feedback= "YouMustBeAnAdminToUpdateaBug"

Returnfalse

}

/ / you can update the bug now

Result=db_query ("UPDATEbug".

"SETfield2='$field2',".

"field3='$field3'".

"WHEREid='$field1'")

/ / now check whether your statement has been executed successfully.

If (! $result) {

/ / updatefailed

Returnfalse

} else {

Returntrue

}

}

? >

three. Easy to modify

Of course you won't use absolute URL throughout the application, but I want to go a step further. The choice of color, element name, font, and other possible options are preferably not absolute, they should be set in a configuration file and included on each page. The style of the site should also be independent-so you don't have to copy and paste every page. I usually put these HTML in a function that can be called when needed.

Database passwords, database connections, etc., are also placed in the database abstraction layer.

four. Object-oriented / function

We can split the process into different function calls. Each call does one thing, and sometimes only needs to call other functions and return the result.

Thank you for reading this article carefully. I hope the article "how to use Smart Architecture in PHP" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report