: This article mainly introduces PHP Design Mode 1: Basic Programming mode. if you are interested in PHP tutorials, refer to it.
Original article address: PHP design mode (1): Basic Programming mode
Introduction
As the saying goes, "PHP is the best language in the world", because PHP can do everything. However, in PHP programming, do you encounter the following confusion: the same requirements are clearly the same, but the code you have previously written cannot be reused. slight modifications do not meet your needs, the big change will make the page change.
Yes, because PHP can do everything, but its high flexibility reduces the code structure. Although the three-party framework can be used to solve the problem, the root cause of the problem lies in the lack of design patterns.
This series of articles will introduce various design patterns from a simple perspective.
Object-oriented programming
Object-oriented Programming, Object-Oriented Programming (OOP), as the most basic design pattern, is not a new topic. However, most of the new PHP Programming programs are writing sequential accounts and splicing strings, so I 'd like to mention it here.
The concept of Object-Oriented Programming is not mentioned here. after all, many people understand how to use it in PHP?
Suppose you need to display different user types on the page, such as computer users and mobile users, then you can abstract the "display" into a class, such:
agent = $_SERVER['HTTP_USER_AGENT']; echo $this->agent; }}$showAgent = new ShowAgent();?>
Debugging skills
In many default PHP environments, the debugging function is disabled. To enable the debugging function, you need to configure the php. ini file. In fact, there is a simple method:
Add this code to your code, and you can even add require or include it to facilitate debugging.
Sequential account programming
It is not intended for you to learn to list sequential programming, but to indicate which programming is not recommended:
There is nothing wrong with this code, but it can no longer be reused, right? Every time you encounter the same problem, you need to repeat it.
Process-oriented programming
Process-oriented programming has been very popular, and its disadvantage is that it cannot be maintained. for example:
This code is also correct, but for a long time, due to the lack of the concept of classes, showTotal lacks flexibility in various application scenarios, you still need to rewrite the code.
Summary
It takes a long time to change the thinking of programming, but remember: algorithms increase the speed of running programs, while design patterns increase the programming speed.
The above introduces the PHP Design Mode 1: The basic programming mode, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.