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

Source: Internet
Author: User
Tags sprintf user definition
I see someone criticizing PHP, what this place is not good for, that place is not good to use. In fact, strictly speaking, there is no language to use, there is no language has a strict standard, everything has a development process, we can not wait for these standards ah what are perfect again use it? I think no matter what language, writing procedures to rely on their own, a programmer to have a good style, ideas and so on. Recently I am in the collation of some information, now issued some, I hope you have more suggestions, more support ah ha

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

First, the environment
Servers: Linux (Apache 2.x, mysql4.1.x, PHP4, Perl, SHELL, CVS, Sambar)
Client: Windows (Ie6, UltraEdit, other assistive tools)
Test machine: Windows98/2k/xp/linux (Ie5, Ie6, Mozilla, Firefox)

Three layers of web pages, programs, and databases
The so-called Web page is not a general static page, where the page is based on the project analysis of the specific circumstances of the split
After using HTML template; Here the database includes the database and other parts of the interface program, usually the program and the database
The program may be mixed in a file, but it should be separated by the function of the method, if the other programs need to use the number
These functions are directly called by the library and cannot be directly exposed to SQL statements.

Three, the project analysis--Data analysis
After a project has been given a demand analysis, the first step before actual development should be data analysis. Data analysis is
The various types of data to be used in the course of the project are piled together, classified according to their characteristics and organized separately, when
However, there may be a variety of associations between them. This step makes the project analysis work get a good
Good beginning, for the following project structure analysis and data processing process analysis also provides a great convenience.

Iv. Project ANALYSIS--Data abstraction
After analyzing the data, we should be able to have some rough data model and some basic data small model combination in our brain.
into a large model, in general, we set up some data needs to be changed to maintain the database, do not need to change
Data types, and to abstract the relevant classes for the data type, and to establish the phase of the database operation
Interface (function form, or method), the operation of data associated with data can also abstract some basic methods,
We only need to make calls in the program design.

V. Project ANALYSIS--Interface analysis
We analyze the data, the purpose is to assemble one or several products, and since the product to be done to others to see.
So we have to do the interface design, when the various interfaces as far as possible to consider the overall, the design of the interface to create a template, and
Write the corresponding processing interface program (so, in the program eye, the interface is also a kind of data), when writing programs to use.

VI. Project ANALYSIS--Process design
The site-style program is very simple, according to the process call us to design a variety of data can be.

Vii. Case Studies
User system, now we analyze one of the simplest examples of a user system.
1. Data analysis, we analyze one of the simplest user systems, so there are only two data, that is the user name
and password, continue to analyze also think we should give each record a number (ID), now has three data, really did not
There can be added again.
2. Data abstraction, only three data model, think of its possible operation methods, we do the following arrangements,
Database Interface (Savetodb (), Getfromdb (), delete ()), data warehousing and outbound and delete; change secret
Code (password ()). Also considering the management and viewing of the user system, there will be a collection type of data (list).
3. Interface analysis, login, verification success, verification error, change password, change password success, change password error, with
Registration, registration error, management--user list, management--User information view, management--Modify the user
Password, manage--delete user.
4. Sample Code
PHP Code:
Copy CodeThe code is as follows:


Include_once "include.php";
/*
* * Use: User system data abstraction
* * Author: Ye Shinming
* * Time: 2005-8-30-10:05
*/
Class User {
var $id = 0;
var $Name = "";
var $Password = "";

var $db = "";
var $tpl = "";

/*
* * Function Function: constructor, specifies the database connection used by the class
* * Parameter Description: $TPL, display template handle; $userdb, database connection
* * Return value: None
* * Author: Ye Shinming
* * Created time: 2005-8-30 10:37
*/
function User ($vtpl = "", $userdb = "") {
if ($vtpl = = "") {
Global $TPL; Externally defined database connections
$this->tpl =& $tpl;
} else {
$this->tpl = $VTPL;
}
if ($userdb = = "") {
Global $db; Externally defined database connections
$this->db =& $db;
} else {
$this->db = $userdb;
}
}
/*
* * Function function: storing data in database
* * Parameter description: no parameter
* * return value: True/false, Success/failure
* * Author: Ye Shinming
* * Created time: 2005-8-30 10:24
*/
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 function: Get the record from the database
* * Parameter description: $id, record number
* * return value: True/false, Success/failure
* * Author: Ye Shinming
* * Created time: 2005-8-30 10:32
*/
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 function: Delete records from the database
* * Parameter description: $id, record number
* * return value: True/false, Success/failure
* * Author: Ye Shinming
* * Created time: 2005-8-30 10:47
*/
function Delete ($id = 0) {
if (Is_array ($id)) {//delete more than one record at a 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 function: Display login interface
* * Parameter Description: $placeholder, display location
* * Return value: None
* * Author: Ye Shinming
* * Created time: 2005-8-30 11:00
*/
function Showlogin ($placeholder) {
$this->tpl->addblockfile ($placeholder, "User_showlogin",
"Tpl.user_showLogin.html"
);
$this->tpl->setcurrentblock ("User_showlogin");
$this->tpl->setvariable (Array ("User_logintitle" and "User login",
"strUserName" = "User name",
"Strpassword" = "Password"
)
);
$this->tpl->parsecurrentblock ("User_showlogin");
}

/*
* * Function function: Processing login information
* * Parameter Description: $placeholder, display location
* * return value: True/false, Success/failure
* * Author: Ye Shinming
* * Created time: 2005-8-30 11:12
*/
function GetLogin ($placeholder = "") {
if (isset ($_post["Login")) {
if ($_post["username"] = = "") {
if ($placeholder! = "") {
$this->tpl->setvarable ($placeholder, "User name cannot be empty! ");
}
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 function: Display registration interface
* * Parameter Description: $placeholder, display location
* * Return value: None
* * Author: Ye Shinming
* * Created time: 2005-8-30 13:33
*/
function Showregister ($placeholder) {
$this->tpl->addblockfile ($placeholder, "User_showregister",
"Tpl.user_showRegister.html"
);
$this->setcurrentblock ("User_shoregister");
Complete the code to process the template here
...

$this->parsecurrentblock ("User_shoregister");
}

/*
* * Function Function: Process registration information
* * Parameter Description: $placeholder, display location
* * return value: True/false, registration succeeded/failed to register
* * Author: Ye Shinming
* * Created time: 2005-8-30 15:49
*/
function Getregister ($placeholder = "") {
if (Isset ($_post["register")) {
if ($_post["username"] = = "") {//user name Legality check, can be changed to other inspection methods
if ($placeholder! = "") {//Error prompt
$this->tpl->setvariable ($placeholder, "The user name is not legal! ");
}
return false;
}
if ($_post["password"]! = $_post["Repassword"]) {//password legality check
if ($placeholder! = "") {//Error prompt
$this->tpl->setvariable ($placeholder, "two times the input password is 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;
}
}
}//class user definition End

/*
* * Use: User system data List abstract
* * Author: Ye Shinming
* * Time: 2005-8-30-17:21
*/
Class UserList {
var $page = 0;
var $pages = 0;
var $pagesize = 9;
var $recordsum = 0;
var $Users = array ();

var $c;
var $db = "";
var $tpl = "";

/*
* * Function Function: constructor, initializes some variables when creating a new class
* * Parameter description: no parameter
* * Return value: None
* * Author: Ye Shinming
* * Created time: 2005-8-30 15:49
*/
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 function: Display list
* * Parameter Description: $placeholder, display location
* * Return value: None
* * Author: Ye Shinming
* * Created time: 2005-8-31 9:16
*/
function Showuserlist ($placeholder) {
$this->tpl->addblockfile ($placeholder, "Showuserlist", "tpl.showUserList.html");
$this->tpl->setcurrentblock ("showuserlist");
Add the appropriate 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="
);
Show Table Header
foreach ($strTitles as $title) {
$this->tpl->setcurrentblock ("Showrecordstitle");
$this->tpl->setvariable ("Strhead", $title);
$this->tpl->parsecurrentblock ("Showrecordstitle");
}
Show records and related actions
if (Is_array ($this->users)) {//has a record
foreach ($this->users as $user) {
$this->tpl->setcurrentblock ("Showrecords");
$this->tpl->setcurrentblock ("Showcell");
$this->tpl->setvariable ("Strcell", $user);
$this->tpl->parsecurrentblock ("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->parsecurrentblock ("showoperations");
}
$this->tpl->parsecurrentblock ("Showcell");
$this->tpl->parsecurrentblock ("Showrecords");
}
} else {//No records
$this->tpl->setcurrentblock ("Showrecords");
$this->tpl->setcurrentblock ("Showcell");
$this->tpl->setvariable ("Strcell", "no Record");
$this->tpl->parsecurrentblock ("Showcell");
$this->tpl->setcurrentblock ("Showcell");
$this->tpl->setvariable ("Strcell", "" ");
$this->tpl->parsecurrentblock ("Showcell");
$this->tpl->parsecurrentblock ("Showrecords");
}
$this->tpl->setcurrentblock ("Showpageinfo");
$this->tpl->setvariable (Array ("Intcolspan" = "2",
"Intrecordsum" = $this->recordsum,
"Intpage" = $this->page,
"Intpages" = $this->pages
)
);
$this->tpl->parsecurrentblock ("Showpageinfo");
$this->tpl->parsecurrentblock ("showuserlist");
}
}
?>

HTML Code:
<textarea id="runcode83028">{strtitle} <table width= "100%" cellspacing= "0" cellpadding= "8" border= "1" > <tr bgcolor= "#dfe0bb" &G T <TD align= "center" >{strHead}</td> </tr> <tr> <td> {Strcell} {strOp Eration} </td> </tr> <tr> <td colspan= "Intcolspan" > Total {intrecordsum} entries, {INTPAGE}/{INTP Ages} page {Intpagenum} </td> </tr> </table></textarea>
[Ctrl + A full selection Note: If you need to introduce external JS need to refresh to execute]

http://www.bkjia.com/PHPjc/317998.html www.bkjia.com true http://www.bkjia.com/PHPjc/317998.html techarticle I see someone criticizing PHP, what this place is not good for, that place is not good to use. In fact, strictly speaking, no language is easy to use, there is not a language has a strict standard, ...

  • Related Article

    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.