search
HomeBackend DevelopmentPHP TutorialGradually improve the performance of the PHP framework_PHP tutorial

Gradually improve the performance of the PHP framework_PHP tutorial

Jul 21, 2016 pm 03:54 PM
cphponehostin whatperformancepromoteframeofat presentquestion

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
What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

How do you optimize PHP applications for performance?How do you optimize PHP applications for performance?May 08, 2025 am 12:08 AM

TooptimizePHPapplicationsforperformance,usecaching,databaseoptimization,opcodecaching,andserverconfiguration.1)ImplementcachingwithAPCutoreducedatafetchtimes.2)Optimizedatabasesbyindexing,balancingreadandwriteoperations.3)EnableOPcachetoavoidrecompil

What is dependency injection in PHP?What is dependency injection in PHP?May 07, 2025 pm 03:09 PM

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

Best PHP Performance Optimization TechniquesBest PHP Performance Optimization TechniquesMay 07, 2025 pm 03:05 PM

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

PHP Performance Optimization: Using Opcode CachingPHP Performance Optimization: Using Opcode CachingMay 07, 2025 pm 02:49 PM

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor