PHP application architecture

Source: Internet
Author: User
Although the name is "PHP best practices", it focuses not on programming rules, but on the rational architecture of PHP applications. It provides an architecture mode for logical and data separation and is a practice of the MVC mode. I think this is a very useful learning material. there are not many similar articles online, so I translated it as I learned it. "> <LINKhref =" http://www.php100.com

Although the name is "PHP best practices", it focuses not on programming rules, but on the rational architecture of PHP applications.

It provides an architecture mode for logical and data separation and is a practice of the MVC mode. I think this is a very useful learning material. there are not many similar articles online, so I translated it as I learned it.

Based on my understanding, I have summarized the implementation of its MVC mode (for details, see the translation ):

* View: front-end webpage;

* Logic layer (Controller): Page Controller is used to process Page requests. then, Business Controller is called to implement specific functions;

* Data Layer (Model): The data is stored in the database. there is a database abstraction layer on top, and a "Data Access Object" (DAO) on top ), it generates a "Value Object" (Value Object ). The business logic uses DAO to operate value objects.

========================================================

PHP best practices

Original: http://www.odi.ch/prog/design/php/guide.php

Translator: Ruan Yifeng

This article provides solutions to common PHP programming problems and briefly describes the PHP application architecture.

1. php. ini settings

Php. ini controls the interpreter's behavior. the following settings ensure maximum portability of your program.

I. short_open_tag

If it is set to 0, PHP's long tag format is always used: no short tag format is required.

Ii. asp_tags

Set to 0 without ASP labels.

Iii. magic_quotes_gpc

We recommend that you include a global file in the script. before reading the $ _ GET, $ _ POST, and $ _ COOKIE variables, check whether the setting is enabled. If yes, this applies the stripslashes function to these variables. (Note: This setting has been abolished in PHP 5.3 .)

Iv. register_globals

Do not rely on this setting. always use the global variables $ _ GET, $ _ POST, and $ _ COOKIE to read GET, POST, and COOKIE values. For convenience, we recommend that you 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) to allow upload.

* Memory_limit must be slightly larger than post_max_size and upload_max_filesize.

* Post_max_size and upload_max_filesize must be large enough to meet the upload needs.

2. configuration file)

You should write all the configurations related to the application in a file. In this way, you can easily adapt to the changes in the development environment. The configuration file usually contains the following information: database parameters, email addresses, various options, debug and logging output switches, and application constants.

3. namespace)

When selecting the class and function name, you must be careful to avoid duplicate names. Do not place global functions outside the class as much as possible. internal attributes and methods of the class are protected by namespaces. If you do need to declare global functions, use a prefix, such as dao_factory (), db_getConnection (), text_parseDate (), and so on.

4. database abstraction layer

PHP does not provide common functions for database operations. each database has its own function. You should not directly use these functions. Otherwise, once you switch to another database (for example, from MySQL to Oracle), you will have a lot of trouble. In addition, the database abstraction layer is generally easier to use than the database functions of the system.

5. "Value Object" (VO)

The value object (VO) is in the form, just like the struct structure in C language. It is a class that only contains attributes and does not contain any methods (or contains only a few methods. A value object corresponds to an object. Its attribute should be the same as the field name of the database. In addition, there should be an ID attribute.

Class Person {

Var $ id, $ first_name, $ last_name, $ email;

}

6. Data Access Object (DAO)

The role of a Data Access Object (DAO) is to isolate database access from other code. DAO should be stacked, which will help you add Database cache in the future. Classes of each value object should have their 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

}

}

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.