Improve the performance of PHP framework _php Tutorial

Source: Internet
Author: User
Tags php framework zend framework
I. What is wrong with the current framework

At present, the mainstream framework Zend Framework, cakephp and so on have adopted the MVC pattern, and implemented the route allocation of the URL. For example, Http://www.xxx.com/user/login maps to the Loginaction method in the Usercontroller object, http://www.xxx.com/user/ Register corresponds to the Registeraction method in the Usercontroller object. This is likely to be the case for the corresponding Usercontroller object.

Class Usercontroller extends controller{

function Loginaction () {
Login
}

function Registeraction () {

}
}
?>

So what kind of problem is there? It's obvious: it contains unnecessary code! For example, when you visit/user/login there is absolutely no need to include the contents of the Registeraction () method. The above code is just a simple example, in general the controller corresponds to a small function module, it will have more functional operations, especially in larger projects. Thus, if there are more than 10 methods in a controller, then each request contains a lot of redundant code. And in PHP performance improvement is very important point: try to avoid including irrelevant code!
In my recent small project, I used my own Phpbean framework (frameworks like the Zend Framework), and in the later development I found that each controller contained too many actions, and later had to consider diverting. But it's not ideal. Project address: http://www.songjin.net:8080.

Second, the problem is not the face of the object of the wrong
Many people think that "there is an error in the face of an object that contains redundant redundancy code," I disagree. As I said in the previous article: The face of the object can achieve all the functions of the process, and do better! The key is to adopt the object-facing thinking to use the face of the object, rather than the face of the process of thinking to write the object-facing program.

Third, how to solve this problem?
The key to the solution is to separate the action. How to separate it? The first thing to know is the role of controller. Controller is the director, the main is to do the request forwarding, the HTTP request forwarded to the specific action. Note: Controller files are not present in struts (note that there is no controller) and it is mapped directly to the action file. So we can put the controller directly into the routing and forwarding, and the real process control, logic processing and so put into action.
For example above, we can split into two files:
loginaction.php

Class Loginaction extends action{

function Run () {

}

}
?>

and registeraction.php

Class Registeraction extends action{

function Run () {

}

}
?>

This enables the separation of the action. When you access the/user/login request, it will not contain the Registeraction code.
But then there are two questions:
First, the actual project in the action file will be very much, how to effectively manage is a key.
Second, the operation in the same function module may have common code, how to share?

The first problem is better solved. Put the action of the same module in a subfolder, that is, allow a multilevel directory. 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 hassle of routing allocation, how to achieve it depends on the reader to think.

The second problem is not difficult to solve, the key is to have an object-oriented thinking. Here, we can use the inheritance of the object to implement. For example above, we can define a user abstract class first.

Class User extends Action () {

function __contruct () {
such as permission checks.
}
}
?>

Then let Loginaction and Registeraction both inherit with user. This will be a good solution.

Iv. Summary
The solution above is just my thoughts for the past few days and may not be perfect. We can refine and optimize the concrete application. For MVC, the framework I always think that in PHP5, objects are more suitable than procedures and more efficient (except for the cost of creating the object itself). For the use of functions to implement the framework, the previous paragraph in pcti Lectures I also tried, I think the idea is similar, but I prefer the object.
Finally, the solution above refers to some of the ideas of struts in Java. Thank you for that!

http://www.bkjia.com/PHPjc/318515.html www.bkjia.com true http://www.bkjia.com/PHPjc/318515.html techarticle first, the current framework of what is the problem the mainstream framework zendframework, cakephp and so on have adopted the MVC pattern, and implemented the URL routing assignment. For example, Http://www.xxx.com/user/logi ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.