search
HomeBackend DevelopmentPHP Tutorial网站架构 - php框架的一些疑惑

我是几年前写着struts一路走来的,后来开始用php,喜欢php的简单。

随着项目参与人员的增长,必须要有规范,要有流程,所以开始研究了一些框架,自己也封装了简单的router,mvc部分。

最近在看另一个php的框架的时候,突然感觉有点疑惑。

在类似java这种应用程序中,我们封装的框架部分可以一直持久在内存中,不必每次加载。但是php的机制是一次请求一次加载。那么每一个请求,都要处理加载配置文件,加载一堆封装的东西是不是有点太浪费了。

那是不是php也会把一些东西放在内存呢?对于这一块请大牛给解释解释。

回复内容:

我是几年前写着struts一路走来的,后来开始用php,喜欢php的简单。

随着项目参与人员的增长,必须要有规范,要有流程,所以开始研究了一些框架,自己也封装了简单的router,mvc部分。

最近在看另一个php的框架的时候,突然感觉有点疑惑。

在类似java这种应用程序中,我们封装的框架部分可以一直持久在内存中,不必每次加载。但是php的机制是一次请求一次加载。那么每一个请求,都要处理加载配置文件,加载一堆封装的东西是不是有点太浪费了。

那是不是php也会把一些东西放在内存呢?对于这一块请大牛给解释解释。

首先不得不吐槽下,php在这方面真的“很弱”,“很弱”并不代表php真很恨“差劲”,不可否认,php是最好的动态模版语言。
php是动态的解释型语言,在语言这个角度提供的功能非常有限,大概这也就是为什么都感觉php上手非常容易的原因吧。于是我们摸索/探索,总结出了很多优秀的框架(Yii, Zend Framework...),集合了很多牛逼的的技术(缓存/队列/集群/复制集/异步/配置系统/日志系统/监控系统/自动化...)到了这个层面也就是出现了楼主的疑惑(php框架的一些疑惑),首先要恭喜你,你最少到了php开发的中级水平,好多人都不敢尝试。这种疑惑是正常反应。因为理解的多了,你就会想整个过程,如果每次请求处理都这样,确实有点效率底下,php在代码层次(PHP5统称OP+)提供了加速器/缓存器,据说可以提高30%+以上的性能,用过的可以pass过。其实早在几年前laruence就反思过这个问题,并开发出了Yaf(框架在php启动的时候已经驻留在内存中了),这里我就不多讲Yaf,有兴趣你可以去看下。

apc、 zend optimizer plus、也就是 php 5.5 起的 opcache 。
都是预编译文件并把 opcode 缓存在内存中, 这是解决你困惑的常规方法, 建议适当使用, 可以给 php 提速 20 到 300 个百分点。
但对于无论 php 还是 java, 瓶颈其实都在数据库 io 上, 把文件加载、配置步骤省去之后,一般提速都不会太多 20 到 100 个百分点, 尤其是数据库操作频繁的。

讲的主要针对的是你提问,其他不相关的就不涉猎。

PHP 本身是不会常驻缓存的,一个请求结束,PHP也就停止执行了。这正是PHP的特点,无须考虑内存、并发等等等东西。
不过你所说的

“那么每一个请求,都要处理加载配置文件,加载一堆封装的东西是不是有点太浪费了。”

并不完全准确。主流的PHP框架都是有缓存的,缓存机制避免了这个问题。

PS:看到没人回答,估计是被楼主写的 “对于这一块请大牛给解释解释。” 吓跑了,我不是大牛,普通 PHP 程序员一名,特此声明。

php有类似java字节码的opcode cache机制,server中运行的php文件会以编译后的opcode的形式缓存在内存中

常见的opcode cache参考 维基:PHP加速器列表

5.5后PHP官方内置了Zend Optimizer。

所以顺便建议配置文件、模版等都以php格式存在,以最大化利用opcode cache,减少磁盘IO

可以把一些固定的配置项写到apache的环境变量里面,php里面用 $_ENV来读取。apache每fork出一个进程,就会带上这些配置,而且是常驻内存的。

opcode是一种思路,accelerate之类的也还可以。只是听大牛的分享里面讲过。http://www.infoq.com/cn/presentations/php-project-control

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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

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 Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),