Home >Backend Development >PHP Tutorial >Gradually improve the performance of the PHP framework_PHP tutorial

Gradually improve the performance of the PHP framework_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:54:25774browse

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!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318515.htmlTechArticle1. What are the problems with the current framework? The current mainstream frameworks, ZendFramework, Cakephp, etc., all adopt the MVC model and implement URL routing assignment. For example, http://www.xxx.com/user/logi...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn