Although the name is "PHP best practice," it mainly talks about not programming rules, but the logical architecture of PHP applications.
It provides a logical and data separation architecture pattern, which is a practice of the MVC pattern. I think, this is a reference value of learning materials, similar articles on the internet is not much, so while learning, one side to translate it out.
Based on my own understanding, I have summed up how it is implemented in the MVC pattern (see translation in detail):
* View level: front page;
* Logic Layer (Controller): first page controller, responsible for processing page requests, and then call the business logic controller to achieve specific functions;
* Data Layer (Model): The data is stored in the database, which has a database abstraction layer, and above it is a "Data Access Object" (DAO), which produces a "value object" (Values objects). The business logic operates on the value object through DAO.
=======================================
PHP Best Practices
Original load: http://www.odi.ch/prog/design/php/guide.php
Translator: Ruan Yi Feng
This paper gives a solution to the common problems in PHP programming, and briefly describes the architecture of the PHP application.
1. PHP.ini Settings
PHP.ini controls the behavior of the interpreter, and some of the following settings ensure that your program has maximum portability.
I. Short_open_tag
Set to 0, that is, always use PHP's long label form: Without the short label form.
II. asp_tags
Set to 0, do not use ASP tags.
III. MAGIC_QUOTES_GPC
It is recommended to include a global file in the script, which is responsible for checking that the setting is open before reading the $_get, $_post, $_cookie variables, and applying the stripslashes function to these variables if it is open. (Note: This setting has been revoked in PHP 5.3.) )
Iv. register_globals
Do not rely on this setting to always read the values of Get, POST, and cookie through global variables $_get, $_post, $_cookie. For convenience, it is recommended to declare $php_self = $_server[php_self].
V. file_uploads
The maximum size of the uploaded file is determined by the following settings:
* File_uploads must be set to 1 (default), which means upload is allowed.
* Memory_limit must be slightly larger than post_max_size and upload_max_filesize.
* Post_max_size and upload_max_filesize to be large enough to meet the needs of uploading.
2. configuration files (config file)
You should write all the configuration related to the application in a file. This makes it easy for you to adapt to changes in your development environment. A configuration file typically contains the following information: database parameters, email addresses, various options, debug and logging output switches, application constants.
3. Namespaces (namespace)
When choosing a class and function name, you must be careful to avoid duplicate names. As far as possible, do not place global functions outside of the class, the properties and methods within the class are equivalent to a layer of namespace protection. If you do need to declare a global function, use a prefix such as dao_factory (), Db_getconnection (), Text_parsedate (), and so on.
4. Database Abstraction Layer
PHP does not provide a common function for database operations, and each database has its own set of functions. You should not use these functions directly, otherwise you will be annoyed if you switch to a different database (for example, from MySQL to Oracle). Moreover, the database abstraction layer is usually easier to use than the database functions of the system itself.
5. "Value Objects" (Value object, VO)
The value object (VO) is formally, like the struct structure of the C language. It is a class that contains only properties, does not contain any methods (or contains only a few methods). A value object that corresponds to an entity. Its properties, typically, should remain the same as the field names of the database. Additionally, you should have an id attribute.
Class Person {
var $id, $first _name, $last _name, $email;
}
6. Data Access Object
The role of Data Access Objects (DAO) is primarily to isolate database access from other code. DAO should be superimposed (stacked) so that you can add the database cache again in the future. Each value object's class should have its own DAO.
Class Persondao {
var $conn;
Function Persondao (& $conn) {
$this->conn =& $conn;
}
Function Save (& $vo) {
if ($v->id = = 0) {
$this->insert ($VO);
} else {
$this->update ($VO);
}
}
function Get ($id) {
#execute SELECT statement
#create new VO and call Getfromresult
#return VO
}
function Delete (& $vo) {
#execute DELETE statement
#set ID on VO to 0
}
#--Private Functions
function Getfromresult (&vo, $result) {
#fill VO from the database result set
}
Function Update (& $VO) {
#execute UPDATE statement here
}
function Insert (& $VO) {
#generate ID (from Oracle sequence or automatically)
#insert record into DB
#set ID on VO
}
}
http://www.bkjia.com/PHPjc/486028.html www.bkjia.com true http://www.bkjia.com/PHPjc/486028.html techarticle Although the name is "PHP best practice," it mainly talks about not programming rules, but the logical architecture of PHP applications. It provides a logical and data separation architecture pattern, belonging to ...