Gradually improve the performance of the php Framework

Source: Internet
Author: User
The above solution is just my thoughts over the past few days and may not be perfect. Specific applications can be further refined and optimized. For MVC and frameworks, I always think that in PHP5, objects are more suitable and effective than processes (excluding the cost of creating objects ). I also tried to use functions to implement the framework in my previous PCTI lectures. I think the ideas are similar, but I prefer objects.

I. What are the problems with the current framework?

Currently, mainstream frameworks such as Zend Framework and Cakephp all adopt the MVC pattern and implement URL routing allocation. For example. In this way, the corresponding userController object is probably like this.


  

So what problems does this have? Obviously: contains unnecessary code! For example, you do not need to include the content in the registerAction () method when accessing/user/login. The above code is just a simple example. Generally, the controller corresponds to a small function module, which has many function operations, especially in large projects. In this way, if a controller contains more than a dozen methods, each request contains a lot of redundant code. An important aspect in improving PHP performance: avoid including irrelevant code as much as possible!

In my recent small project, I used my own phpbean Framework (the Framework is similar to Zend Framework). later on development, I found that each controller contains too many actions, and I had to consider shunting. But not ideal.

2. the problem is not caused by the error of the object.

Many people think that "containing redundant code is an object error." I disagree. As I mentioned in the previous article: all the functions of the process can be implemented for objects, and the process can be better done! The key is to use the object-oriented thinking, rather than the process-oriented thinking to write the object-oriented program.

3. how can this problem be solved?

The key to solving this problem is to separate actions. How to separate it? First, you must understand the role of the controller. The controller is a controller that forwards requests to specific actions. Note: In struts, there is no controller file (note that there is no controller). it is directly mapped to the action file. Therefore, we can directly put the controller in route forwarding, and put real process control and logic processing into action.

For example, in the above example, we can split the file into two files:


loginAction.php
  

And


registerAction.php
  

In this way, the separation of actions is realized. The registerAction code is not included when you access the/user/login request.

However, there are two other problems:

First, there will be a lot of action files in the actual project, and how to effectively manage them is critical.

2. operations in the same function module may share code. how can this problem be shared?

The first problem is better solved. Put the actions of the same module in a sub-folder, that is, multi-level directories are allowed. For example, we can put loginAction. php and registerAction. php in the above code into the user directory. However, note that this will increase the trouble of route allocation, and the readers will think about how to implement it.

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


  

Then, both loginAction and RegisterAction are inherited from the user. This will solve the problem well.

IV. Summary

The above solution is just my thoughts over the past few days and may not be perfect. Specific applications can be further refined and optimized. For MVC and frameworks, I always think that in PHP5, objects are more suitable and effective than processes (excluding the cost of creating objects ). I also tried to use functions to implement the framework in my previous PCTI lectures. I think the ideas are similar, but I prefer objects.

Finally, the above solution references part of struts ideas in java. Thank you




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.