A good object-oriented PHP development model (abbreviated version) _ PHP Tutorial

Source: Internet
Author: User
Tags user definition
A good object-oriented PHP development model (abbreviated version ). I have seen that some people criticize PHP and it is difficult to use it. In fact, strictly speaking, no language is easy to use, and no language has a strict standard. I have seen that some people criticize PHP and it is not easy to use it. In fact, strictly speaking, there is no language that is easy to use, no language that has a strict standard, and everything has a development process, we can't always wait for these standards, and we can use them all very well, right? I think that no matter what language you use, writing programs depends on yourself. a programmer must have a good style and ideas. Recently, I have been sorting out some materials and have sent out some documents. I hope you will give more comments and provide more support.

==============================================
Object-oriented PHP development model (to be improved ...)
==============================================

I. environment
Server: Linux (Apache 2.x, MySQL4.1.x, PHP4, Perl, SHELL, CVS, Sambar)
Client: Windows (Ie6, UltraEdit, and other auxiliary tools)
Testing Machine: windows98/2 K/xp/Linux (Ie5, Ie6, mozilla, firefox)

2. three layers of web pages, programs, and databases
The so-called webpage is not a general static webpage. the webpage here is split according to the specific situation of project analysis.
Templates made with html; here the database includes the database and interface programs with other parts, usually programs and databases
Programs may be mixed in a file, but they should be separated as much as possible using functions.
The database can directly call these functions and cannot directly access SQL statements.

III. project analysis-data analysis
After a project obtains the requirement analysis, data analysis is the first step before actual development. Data analysis is
Pile up all kinds of data used in the project process, classify them according to their characteristics, and then organize them separately.
However, there may be various associations between them. By doing this well, the project analysis work has been well developed.
A good start provides great convenience for the following project structure analysis and data processing process analysis.

IV. project analysis-data abstraction
After data analysis, we should be able to see some general data models and some basic data small model combinations in our minds.
Generally, we create a database to maintain the data that needs to be changed.
Make the data into constants, abstract the related classes for these data types, and establish a phase for database operations.
Interface (function form, that is, method). operations related to data can also abstract some basic methods,
We only need to call it in the program design.

5. project analysis-Interface Analysis
After analyzing the data, we aim to combine one or more products, and now we have to show them to others.
Therefore, we also need to design the interface. when all kinds of interfaces are considered as comprehensive as possible, we will make the design interface into a template, and
Write the corresponding processing interface program (so, in the eyes of the program, the interface is also a type of data), used when writing the program.

6. project analysis-Process Design
Website programs are very simple. just call the data we designed according to the process.

VII. case analysis
The user system is now the simplest example of a user system.
1. data analysis: we analyze a simple user system, so there is only two data here, that is, the user name.
And password, continue analysis will also think that we should add a number (id) for each record, now there are three data, it is not
Can be added again.
2. Data Abstraction. there are only three data models. let's make the following arrangement to think of the possible operation methods,
Database interfaces (savetodb (), getfromdb (), and delete () are stored in the database and deleted in the database; change the password
Password ()). In addition, considering the management and viewing of the user system, there will be a collection type of data (list ).
3. Interface Analysis, login, verification successful, verification error, password modification, password modification successful, password modification error, use
User registration, registration successful, registration error; management-user list, management-user information view, management-modify user
Password, manage -- delete a user.
4. Sample code
PHP code:

The code is as follows:




Include_once "include. php ";
/*
** Purpose: abstract User System data
** Author: Yue shinming
** Time:
*/
Class User {
Var $ id = 0;
Var $ Name = "";
Var $ Password = "";

Var $ db = "";
Var $ tpl = "";

/*
** Function: constructor that specifies the database connection used by the class.
** Parameter description: $ tpl, display the handle of the template; $ userdb, database connection
** Return value: None
** Author: Yue Siming
** Creation time:
*/
Function User ($ vtpl = "", $ userdb = ""){
If ($ vtpl = ""){
Global $ tpl; // external definition database connection
$ This-> tpl = & $ tpl;
} Else {
$ This-> tpl = $ vtpl;
}
If ($ userdb = ""){
Global $ db; // external definition database connection
$ This-> db = & $ db;
} Else {
$ This-> db = $ userdb;
}
}
/*
** Function: Saves data to the database.
** Parameter description: no parameter
** Return value: true/false, success/failure
** Author: Yue Siming
** Creation time:
*/
Function savetodb (){
If ($ this-> Name = ""){
Return false;
}
If ($ this-> id ){
$ StrSQL = sprintf ("UPDATE user SET Name = '% s', Password =' % s '"
. "WHERE id = '% s '",
$ This-> Name,
$ This-> Password,
$ This-> id
);
} Else {
$ StrSQL = sprintf ("INSERT user (Name, Password )"
. "VALUES ('% s',' % s ')",
$ This-> Name,
$ This-> Password
);
}
If ($ this-> db-> query ($ strSQL )){
Return true;
} Else {
Return false;
}
}

/*
** Function: retrieve records from the database
** Parameter description: $ id, record number
** Return value: true/false, success/failure
** Author: Yue Siming
** Creation time:
*/
Function getfromdb ($ id = 0 ){
If ($ id ){
$ StrSQL = sprintf ("SELECT * FROM user WHERE id = '% s'", $ id );
} Else if ($ this-> id ){
$ StrSQL = sprintf ("SELECT * FROM user WHERE id = '% s '",
$ This-> id
);
} Else if ($ this-> Name! = ""){
$ StrSQL = sprintf ("SELECT * FROM user WHERE Name = '% s '",
$ This-> Name
);
} Else {
Return false;
}
$ This-> db-> query ($ strSQL );
If ($ this-> db-> next_record ()){
$ This-> id = $ this-> db-> f ("id ");
$ This-> Name = $ this-> db-> f ("Name ");
$ This-> Password = $ this-> db-> f ("Password ");

Return true;
} Else {
Return false;
}
}

/*
** Function: Delete a record from a database.
** Parameter description: $ id, record number
** Return value: true/false, success/failure
** Author: Yue Siming
** Creation time:
*/
Function delete ($ id = 0 ){
If (is_array ($ id) {// delete multiple records at the same time
Foreach ($ id as $ I ){
$ StrSQL = sprintf ("delete from user WHERE id = '% s'", $ I );
$ This-> db-> query ($ strSQL );
}
Return true;
} Else if ($ id ){
$ StrSQL = sprintf ("delete from user WHERE id = '% s'", $ id );
} Else if ($ this-> id ){
$ StrSQL = sprintf ("delete from user WHERE id = '% s'", $ this-> id );
} Else {
Return false;
}
$ This-> db-> query ($ strSQL );
Return true;
}

/*
** Function: displays the logon page.
** Parameter description: $ placeholder, display location
** Return value: None
** Author: Yue Siming
** Creation time:
*/
Function showLogin ($ placeholder ){
$ This-> tpl-> addBlockfile ($ placeholder, "user_showLogin ",
"Tpl.user_showLogin.html"
);
$ This-> tpl-> setCurrentBlock ("user_showLogin ");
$ This-> tpl-> setVariable (array ("user_Logintitle" => "user login ",
"StrUsername" => "user name ",
"StrPassword" => "password"
)
);
$ This-> tpl-> parsecurityblock ("user_showLogin ");
}

/*
** Function: process login information
** Parameter description: $ placeholder, display location
** Return value: true/false, success/failure
** Author: Yue Siming
** Creation time:
*/
Function getLogin ($ placeholder = ""){
If (isset ($ _ POST ["login"]) {
If ($ _ POST ["username"] = ""){
If ($ placeholder! = ""){
$ This-> tpl-> setVarable ($ placeholder, "the user name cannot be blank! ");
}
Return false;
}
$ This-> Name = $ _ POST ["username"];
$ This-> getfromdb ();
If ($ this-> Password () === _ POST ["password"]) {
Return true;
}
} Else {
If ($ placeholder! = ""){
$ This-> tpl-> setVarable ($ placeholder, "login failed! ");
}
Return false;
}
}

/*
** Function: displays the registration page.
** Parameter description: $ placeholder, display location
** Return value: None
** Author: Yue Siming
** Creation time:
*/
Function showRegister ($ placeholder ){
$ This-> tpl-> addBlockfile ($ placeholder, "user_showRegister ",
"Tpl.user_showRegister.html"
);
$ This-> setCurrentBlock ("user_shoRegister ");
// Complete the template processing code here
...

$ This-> parsecurityblock ("user_shoRegister ");
}

/*
** Function: Process registration information
** Parameter description: $ placeholder, display location
** Return value: true/false, registration successful/registration failed
** Author: Yue Siming
** Creation time:
*/
Function getRegister ($ placeholder = ""){
If (isset ($ _ POST ["register ")){
If ($ _ POST ["username"] = "") {// check the validity of the username. you can change it to another check method.
If ($ placeholder! = "") {// Error message
$ This-> tpl-> setVariable ($ placeholder, "the user name is invalid! ");
}
Return false;
}
If ($ _ POST ["password"]! = $ _ POST ["repassword"]) {// check the validity of the password
If ($ placeholder! = "") {// Error message
$ This-> tpl-> setVariable ($ placeholder, "the two passwords are inconsistent! ");
}
Return false;
}

$ StrSQL = sprintf ("select count (*) FROM user"
. "WHERE Name = '% s '",
$ This-> Name
);
$ This-> db-> query ($ strSQL );
$ This-> db-> next_record ();
If ($ this-> db-> f ("COUNT (*)")> 0 ){
Return false;
} Else {
$ StrSQL = sprintf ("insert into user (Name, Password )"
. "VALUES ('% s',' % s ')",
$ This-> Name,
$ This-> Password
);
$ This-> db-> query ($ strSQL );
Return true;
}
} Else {
Return false;
}
}
} // End of class User definition

/*
** Purpose: abstract the user system data list
** Author: Yue shinming
** Time:
*/
Class UserList {
Var $ page = 0;
Var $ pages = 0;
Var $ pagesize = 9;
Var $ recordsum = 0;
Var $ Users = array ();

Var $ c;
Var $ db = "";
Var $ tpl = "";

/*
** Function: constructor. some variables are initialized when a class is created.
** Parameter description: no parameter
** Return value: None
** Author: Yue Siming
** Creation time:
*/
Function UserList ($ page = 1, $ pagesize = 10,
$ C, $ vtpl = "", $ vdb = ""){
$ This-> page = $ page;
$ This-> pagesize = $ pagesize;
$ This-> condition = $ condition;
If ($ vdb! = ""){
$ This-> db = $ vdb;
} Else {
Global $ db;
$ This-> db = $ db;
}
If ($ vtpl! = ""){
$ This-> tpl = $ vtpl;
} Else {
$ This-> tpl = $ tpl;
}

$ StrSQL = sprintf ("select count (*) FROM user WHERE '% s '",
$ This-> condition
);
$ This-> db-> query ($ strSQL );
$ This-> db-> next_record ();
$ This-> recordsum = $ this-> db-> f ("COUNT (*)");

$ This-> pages = ceil ($ this-> recordsum/$ this-> pagesize );

$ StrSQL = sprintf ("SELECT * FROM user WHERE '% s' LIMIT' % s', '% s '",
$ This-> condition,
$ This-> page * $ this-> pagesize,
$ This-> pagesize + 1
);
$ This-> db-> query ($ strSQL );
For ($ I = 0; $ this-> db-> next_record (); $ I ++ ){
$ This-> Users [$ I] = new User ($ this-> tpl, $ this-> db );
$ This-> Users [$ I]-> id = $ this-> db-> f ("id ");
$ This-> Users [$ I]-> Name = $ this-> db-> f ("Name ");
$ This-> Users [$ I]-> Password = $ this-> db-> f ("Password ");
}
}


/*
** Function: displays a list.
** Parameter description: $ placeholder, display location
** Return value: None
** Author: Yue Siming
** Creation time:
*/
Function showUserList ($ placeholder ){
$ This-> tpl-> addBlockfile ($ placeholder, "showUserList", "tpl.showUserList.html ");
$ This-> tpl-> setCurrentBlock ("showUserList ");
// Add the corresponding processing code here
$ This-> tpl-> setVariable ("strTitle", "user list ");
$ StrTitles = array ("user name", "operation ");
$ RecordOperations = array ("Reset password" => "operate = passwd & id = ",
"Delete" => "operate = delete & id ="
);
// Display the header
Foreach ($ strTitles as $ title ){
$ This-> tpl-> setCurrentBlock ("showRecordsTitle ");
$ This-> tpl-> setVariable ("strHead", $ title );
$ This-> tpl-> parsecurityblock ("showRecordsTitle ");
}
// Display records and related operations
If (is_array ($ this-> Users) {// record
Foreach ($ this-> Users as $ user ){
$ This-> tpl-> setCurrentBlock ("showRecords ");
$ This-> tpl-> setCurrentBlock ("showCell ");
$ This-> tpl-> setVariable ("strCell", $ user );
$ This-> tpl-> parsecurityblock ("showCell ");
$ This-> tpl-> setCurrentBlock ("showCell ");
Foreach ($ RecordOperations as $ operation => $ linkOperation ){
$ This-> tpl-> setCurrentBlock ("showOperations ");
$ This-> tpl-> setVariable ("strOperation", $ operation );
$ This-> tpl-> setVariable ("strLink", $ _ SERVER ["REQUEST_URI"]. $ linkOperation. $ user-> id );
$ This-> tpl-> parsecurityblock ("showOperations ");
}
$ This-> tpl-> parsecurityblock ("showCell ");
$ This-> tpl-> parsecurityblock ("showRecords ");
}
} Else {// No record
$ This-> tpl-> setCurrentBlock ("showRecords ");
$ This-> tpl-> setCurrentBlock ("showCell ");
$ This-> tpl-> setVariable ("strCell", "no record ");
$ This-> tpl-> parsecurityblock ("showCell ");
$ This-> tpl-> setCurrentBlock ("showCell ");
$ This-> tpl-> setVariable ("strCell ","");
$ This-> tpl-> parsecurityblock ("showCell ");
$ This-> tpl-> parsecurityblock ("showRecords ");
}
$ This-> tpl-> setCurrentBlock ("showPageInfo ");
$ This-> tpl-> setVariable (array ("intColspan" => "2 ",
"IntRecordSum" => $ this-> recordsum,
"IntPage" => $ this-> page,
"IntPages" => $ this-> pages
)
);
$ This-> tpl-> parsecurityblock ("showPageInfo ");
$ This-> tpl-> parsecurityblock ("showUserList ");
}
}
?>


HTML code:

{StrTitle} <table width = "100%" cellSpacing = "0" cellPadding = "8" border = "1"> <tr bgcolor = "# dfe0bb"> <td align =" center "> {strHead} </td> </tr> <td> {strCell} {strOperation} </td> </tr> <td colspan = "intColspan"> a total of {intRecordSum} records, {intPage}/{intPages} page {intPageNum} </td> </tr> </table>
[Ctrl + A Select All Note: If you need to introduce external Js, you need to refresh it to execute]

Bytes. In fact, strictly speaking, no language is easy to use, and no language has a strict standard ,...

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.