This article will give you one minute to learn how to use PHP to build your own web framework? (Sharing) has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
The WEB mentioned here refers to the PHP WEB program running under apache.
We must first understand the operating mechanism of PHP under apache and the life cycle of requests.
PHP is a scripting language. Its execution process starts from the file entry to the end of the file. It can contain or reference other files and is process-oriented. In the process, objects can be used to implement various required logical processing. You can use one or more objects to complete the required functions, and you can also tell an object what function you want to complete. This is an object-oriented development method and a common development method. Therefore, in the process-oriented operating mechanism, use the object-oriented development method.
The life cycle of each HTTP request also starts from the entrance until the end of the program. The variables in it will no longer exist. The variables of different HTTP requests are independent and do not affect each other. We can use global declarations, $GLOBALS global array variables, and static static variables to share data in the same HTTP request; use session to achieve session-level sharing; use cache to achieve site-wide data sharing. The global statement is generally used in methods and is used in process-oriented development. It is not usually used. $GLOBALS and static are often used, but they cannot be operated directly. Instead, they are managed in objects or special methods. For example, the commonly used singleton mode is saved using $GLOBALS and static.
In PHP programs, we will consider making the framework as simple, efficient, clear, and easy to use as possible, which is good for development and maintenance.
The basic program model uses the MVC model, which is layered and divided into modules. At the same time, a useful URL router is required to cooperate with MVC.
URL router: A very critical component that determines the organizational structure of the source code file and the clarity of the code. A good router can easily find the logical entry, reflecting the ease of use of the framework.
Model: Always use arrays. The main concern when using arrays is that the content of the array is unclear. In the project, the attributes refer to database fields, so the content of the array is relatively clear. Model operations are encapsulated using the data access layer DAO. In database access, it is also more efficient to convert directly into array form. For interactive data objects of other systems, there are generally interface document definitions. Regarding the Active Record technology in ORM, it is better not to use it or not.
View: I started using smarty, but in the performance report, the method executed by smarty took too much time. Later, I used tmd_tpl, which is just one file, easy to use and efficient. Easy to modify. In the view template, combine PHP syntax and supplement it with template variables. The idea of MVC is separation, which does not mean that PHP syntax cannot be used in View. If it is an API interface, the data can be directly converted into a specific format result and returned.
Controller: or action represents a behavior, a method, and an interface. Only one layer of controller is often not enough. It is generally divided into an interface layer, a business layer, a data access layer, and a communication layer. The interface is responsible for parameter verification, access permission control, calling specific services, and finally returning data or displaying pages, etc. It is best for all businesses to start with the interface layer. Before that, we should only do framework things. When we need to read a certain business implementation, we only need to start reading along the interface layer entrance. The business layer performs actual business functions. The business layer obtains data from the data access layer and performs business processing. The data access layer obtains data from the database or calling interface and can perform simple data conversion processing. If PHP is only used as the front end for data display, and the back end is executed by C/C/GO, etc., then you only need to encapsulate the business layer, request the data to the back end in the business layer, and then return it to the interface layer.
The above is the basic framework structure of the program, or the process structure of the business, which is usually the most important part of the system. But it is still far from being practical, and there are still many basic functions to be added, such as session processing, database access, log processing and other functions. These basic functions are generally independent of the framework and can be applied on different frameworks. Functional classes should not be too tightly coupled to the framework, and generally use combination methods. We encapsulate these basic functions into core classes in a way that is easy to use, and use single or multiple instances to call them, or further encapsulate the classes into global methods for easy use.
I think the simplest and easiest to use PHP website framework requires a route, a controller and a view template engine. Other functional modules can be added as needed.
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to build your own web framework with PHP?. For more information, please follow other related articles on the PHP Chinese website!