Original address: PHP Design Pattern (1): Basic Programming Pattern
Introduction
As the saying goes, "PHP is the best language in the world" because PHP can do everything. But in PHP programming, will you encounter such confusion: It is obviously the same requirement, but the code written before cannot be reused. Slight modifications will not meet the requirements, and major changes will change the page.
Yes, because PHP can do everything, but the high flexibility reduces the structure of the code. Although the problem can be solved using a tripartite framework, the root of the problem lies in the lack of design patterns.
This series of articles will introduce various design patterns from shallow to deep.
Object-Oriented Programming
Object-Oriented Programming (OOP) as the most basic design pattern is not a new topic, but most novices in PHP programming are writing journals and splicing strings. So I still have to mention it here.
The concept of Object-Oriented Programming will not be discussed here. After all, many people understand it, but how to use it in PHP?
Suppose you need to display different user types on the page, such as computer users, mobile phone users, etc., then you can abstract the "display" into a class, such as:
<code><?php class ShowAgent { private $agent; public function __construct() { $this->agent = $_SERVER['HTTP_USER_AGENT']; echo $this->agent; } } $showAgent = new ShowAgent(); ?></code>
Debugging skills
In many PHP default environments , the debugging function is turned off. Turning on the debugging function requires configuring the php.ini file. In fact, there is a simple method:
<code><?php ini_set("display_errors", "1"); ERROR_REPORTING(E_ALL); ?></code>
Add this code to your code, and you can even require or include it to facilitate debugging.
Journaling Account Programming
Here is a list of journal programming, not for you to learn, but to point out what kind of programming is not recommended:
<code><?php $total = "Total number is "; $number = "6"; $totalNumber = $total.$number; echo $totalNumber; ?></code>
There is nothing wrong with this code, but it can never be reused in the future, right? Every time you encounter the same problem, you need to splice it again and again.
Process-oriented programming
Process-oriented programming was once very popular, but its disadvantage is that it cannot be maintained. For example:
<code><?php function showTotal($total, $number) { $totalNumber = $total.$number; echo $totalNumber; } showTotal("Total number is", "6"); ?></code>
This code is also correct, but over time, due to the lack of the concept of classes, showTotal lacks flexibility in various application scenarios. Yes, you still need to rewrite the code.
Summary
It takes a long time to change your thinking about programming, but remember: algorithms increase the speed of program execution, while design patterns increase the speed of programming.
The above introduces PHP design pattern 1: basic programming pattern, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.