


A good article on object-oriented PHP development model (abbreviated version)_PHP tutorial
I saw some people criticizing PHP, why this part is not easy to use, and that part is not easy to use. In fact, strictly speaking, no language is easy to use, and no language has strict standards. Everything has a development process. We can't wait for these standards to be perfect before using them, right? I think no matter what language you use, you have to rely on yourself when writing programs. A programmer must have a good style, ideas, etc. I've been sorting out some information recently, and I'm sending some out now. I hope everyone can give me more opinions and support.
======================== ===============
Object-oriented PHP development model (to be improved...)
================ =======================
1. Environment
Server: Linux (Apache 2.x, MySQL4.1.x, PHP4, Perl, SHELL, CVS, Sambar)
Client: Windows (Ie6, UltraEdit, other auxiliary tools)
Test machine: windows98/2K/xp/Linux (Ie5, Ie6, mozilla, firefox)
2. Three layers of web pages, programs and databases
The so-called web pages are not ordinary static web pages. The web pages here are templates made of HTML after being split
according to the specific situation of the project analysis; The database here includes database and interface programs with other parts. Usually programs and database
programs may be mixed in one file, but they should be separated as much as possible using functions. If other programs want to use data
The library can directly call these functions and cannot directly access SQL statements.
3. Project Analysis - Data Analysis
After a project has received demand analysis, the first step before actual development is data analysis. Data analysis is to
pile up all kinds of data that will be used in the project process, classify them according to their characteristics and organize them separately. Of course
there may be various kinds of data between them. relationship. Doing this step well will give the project analysis work a good start, and also provide great convenience for the following project structure analysis and data processing process analysis.
4. Project Analysis - Data Abstraction
After data analysis, we should be able to have some rough data models and some basic data small models combined
into a large model in our minds. Generally, In this case, we create a database for maintenance of some data that needs to be changed, and make some constants for the data that does not need to be changed, and abstract relevant classes for these data types, and establish relevant methods for database operations. >Relationship interface (function form, that is, method), data and data-related operations can also abstract some basic methods,
We only need to call them in programming.
5. Project Analysis - Interface Analysis
We have analyzed the data with the purpose of combining one or several products, and since we want to make a product, we need to show it to others.
So we still need to design the interface. After considering the various interfaces as comprehensively as possible, we will make the designed interface into a template, and
write the corresponding processing interface program (so, in the eyes of the program, the interface is also A kind of data), used when writing programs.
6. Project Analysis - Process Design
The website program is very simple, just follow the process to call various data we designed.
7. Case Analysis
User system, now we analyze the simplest example, a user system.
1. Data analysis, we analyze the simplest user system, so there are only two data here, that is the user name
and password. If we continue the analysis, we will also think that we should add a number (id) to each record ), now that there are three data, there is really nothing
to add.
2. Data abstraction, a data model with only three data, thinking of its possible operation methods, we make the following arrangements,
Database interface (savetodb(), getfromdb(), delete()), respectively There is also deletion of data entering and exiting the database; changing 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, change password, change password successfully, change password error, user registration with
, registration successful, registration error; management - user list, management - user Information viewing, management - modify user
password, management - delete user.
4. Sample code
PHP code:
Copy code
include_once "include.php";
/*
** Purpose: User system data abstraction
** Author: Yue Letter
** Time: 2005-8-30 10:05
*/
class User {
var $id = 0;
var $Name = "";
var $Password = "";
var $db = "";
var $tpl = "";
/*
** Function: constructor, specified class Database connection used
** Parameter description: $tpl, display template service handle; $userdb, database connection
** Return value: None
** Author: Yue Xinming
** Created Time: 2005-8-30 10:37
*/
function User($vtpl = "", $userdb = "") {
if ($vtpl == "") {
global $tpl; // Externally defined database connection
$this->tpl =& $tpl;
} else {
tpl = $vtpl; }
if ($userdb == "") {
global $db; // Externally defined database connection
$this->db =& $db;
} else {
$this-> ;db = $userdb;
} : true/false, success/failure
** Author: Yue Xinming
** 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->Password,
$ this->id
else {
$strSQL = sprintf("INSERT user (Name, Password) "
"VALUES ('%s', '%s')",
$this->Name,
, >query($strSQL)) {
return true;
} else {
return false; ; ** Return value: true/false, success/failure
** Author: Yue Xinming
** 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-& gt; id = $ this-& gt; db-& gt; f ("id");
$ this-& gt; name = $ this- & gt; db-& gt; f ("name") ;
$this->Password else {
return false;
}
}
/*
** Function: Delete records from the database
** Parameter description: $id, record number
** Return value: true/false, success/failure
** Author: Yue Xinming
** Creation time: 2005-8-30 10:47
*/
function delete($id = 0) {
if (is_array($id)) { //Delete multiple records at the same time
foreach($id as $i) {
$strSQL = sprintf("DELETE F ROM user WHERE id='%s'", $i);
} else if ($ ID) { $ Strsql = Sprintf ("Delete from User where ID = '%s'", $ ID);
} else if ($ this-& gt; id) {
$ strsql = sprintf ("DELETE FROM user WHERE id='%s'", $this->id); t; query($strSQL);
return true;
}
/*
** Function: Display the login interface
** Parameter description: $placeholder, display position
** Return value: None
** Author: Yue Xinming
** Created time: 2005-8-30 11:00
*/
function showLogin($placeholder) {
$this->tpl->addBlockfile($placeholder, " user_showLogin",
"tpl.user_showLogin.html"
"tpl.user_showLogin.html" );
$this->t pl->setCurrentBlock("user_showLogin");
$this->tpl->setVariable (array("user_Logintitle" => "User login", "strUsername" => "Username",
"strPassword" => "Password"
) 🎜> $this->tpl->parseCurrentBlock("user_showLogin"); placeholder, display position
** Return value: true/false, success/failure
** Author: Yue Xinming
** Creation time: 2005-8-30 11:12
*/
function getLogin($placeholder = "") {
if (isset($_POST["login"])) {
if ($_POST["username"] == "") {
if ($placeholder != "") { ");
> $this->getfromdb();
if ($this->Password() == $_POST["password"]) {
placeholder != "") {
$this->tpl->setVarable($placeholder, "Login failed!"); }
/*
** Function: Display registration interface
** Parameter description: $placeholder, display position
** Return value: None
** Author: Yue Xinming
** Creation time :2005-8-30 13:33
*/
function showRegister($placeholder) {
$this->tpl->addBlockfile($placeholder, "user_showRegister",
"tpl .user_showRegister.html"
... this->parseCurrentBlock("user_shoRegister");
}
/*
** Function: Process registration information
** Parameter description: $placeholder, display position
** Return value: true/false, registration successful/registration failed
** Author: Yue Xinming
** Created time: 2005-8-30 15:49
*/
function getRegister ($placeholder = "") {
if (isset($_POST["register")) {
if ($_POST["username"] == "") { // Username validity check, Can be changed to other checking methods
If ($placeholder != "") { // Error message
$this->tpl->setVariable($placeholder, "The username is illegal!“); ) { // Password validity check
($ Placeholder! = "") {// Error prompts
$ This-& GT; TPL-& GT; Setvariable ($ Placeholder, "Two input 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: User system data list abstraction
** Author: Yue Xinming
** 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: constructor, initialize some variables when creating a new class
** Parameter description: no parameters
** Return value: none
** Author: Yue Xinming
** Creation 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;
} ($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: Display list
** Parameter description: $placeholder, display position
** Return value: None
** Author :Yue Xinming
** Creation time: 2005-8-31 9:16
*/
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("Username", "Operation");
$RecordOperations = array("Reset Password" => "operate=passwd&id=", ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐'s
$this->tpl->setCurrentBlock("showRecordsTitle");
$this->tpl->setVariable("strHead", $title);
$this-> ;tpl ->parseCurrentBlock("showRecordsTitle"); foreach ($ this->Users as $user) {
$this->tpl->setCurrentBlock("showRecords");
$this->tpl->setCurrentBlock("showCell");
$this->tpl->setVariable("strCell", $user); this->tpl-> ;setCurrentBlock("showCell");
$this- >tpl->setVariable("strOperation", $operation); id);
$this->tpl->parseCurrentBlock("showOperations");
$this->tpl->parseCurrentBlock("showOperations");
$this->tpl->parseCurrentBlock("showCell");
$this->tpl->parseCurrentBlock("showRecords");
}
} else { // 无记录
$this->tpl->setCurrentBlock("showRecords");
$this->tpl->setCurrentBlock("showCell");
$this->tpl->setVariable("strCell", "无记录");
$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 代码:
[Ctrl+A to select all Note: If you need to introduce external Js, you need to refresh to execute]

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
