


Gradually improve the performance of the PHP framework_PHP tutorial
1. What are the problems with the current framework?
The current mainstream frameworks, Zend Framework, Cakephp, etc., all adopt the MVC model and implement URL routing distribution. For example, http://www.xxx.com/user/login will be mapped to the loginAction method in the userController object, and http://www.xxx.com/user/register will correspond to the registerAction method in the userController object. The corresponding userController object is likely to be like this.
class userController extends controller{
function loginAction(){
//login
} registerAction(){
It's obvious: unnecessary code is included! For example, when you visit /user/login, there is no need to include the content in the registerAction() method. The above code is just a simple example. Generally speaking, the controller corresponds to a small functional module, which will have more functional operations, especially in larger projects. In this way, if there are more than a dozen methods in a controller, each request will contain a lot of redundant code. A very important point in improving PHP performance: try to avoid including irrelevant code!
In my recent small project, I used my own phpbean framework (the framework is similar to Zend Framework). During subsequent development, I found that each controller indeed contained too many actions, and later I had to consider offloading. But it's far from ideal. Project address: http://www.songjin.net:8080.
2. The problem is not because of the object-oriented error
Many people think that "containing redundant code is an object-oriented error", I disagree. As I said in the last article: object-facing can realize all the functions of process-facing, and do it better! The key is to use object-oriented thinking to use object-oriented thinking, rather than using process-oriented thinking to write object-oriented programs.
3. How to solve this problem?
The key to the solution is to separate actions. How to separate it? First of all, we need to understand the role of controller. The controller is a controller, which mainly forwards requests and forwards http requests to specific actions. Note: There is no controller file in struts (note that this does not mean there is no controller), it is directly mapped to the action file. Therefore, we can put the controller directly into the routing and forwarding, and put the real process control, logic processing, etc. into the action.
For example, in the above example, we can separate it into two files:
loginAction.php
class loginAction extends Action{
function run( ){
}
}
?>
and registerAction.php
🎜>
function run(){
}
}
?>
This achieves the separation of actions. When you access /user/login the request will not contain the registerAction code.
But there are two problems with this:
First, there will be a lot of action files in actual projects, and how to manage them effectively is the key.
Second, operations in the same functional module may have common code, how to share it?
The first problem is easier to solve. Put the actions of the same module into a subfolder, which means that multi-level directories are allowed. For example, in our code above, we can put loginAction.php and registerAction.php in the user directory. But note that this will increase the trouble of route distribution. It is up to the readers to think about how to implement it.
The second problem is not difficult to solve. The key is to have object-oriented thinking. Here, we can use object inheritance to achieve this. For example, in the above example, we can first define a user abstract class.
class user extends Action(){
function__contruct(){
}
?>
Then let both loginAction and RegisterAction inherit from user. This can be solved very well.
4. Summary
The above solutions are just my thoughts in the past few days and may not be perfect enough. Specific applications can be further refined and optimized.Regarding MVC and frameworks, I always think that in PHP5, objects are more suitable and more effective than procedures (excluding the cost of creating the object itself). As for using functions to implement frameworks, I also tried it in the previous PCTI lecture. I think the ideas are similar, but relatively speaking, I prefer objects.
Finally, the above solution refers to some ideas of struts in java. Thank you!

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

AI Hentai Generator
Generate AI Hentai for free.

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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide
