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 vs. Python: Understanding the DifferencesPHP vs. Python: Understanding the DifferencesApr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: Is It Dying or Simply Adapting?PHP: Is It Dying or Simply Adapting?Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The Future of PHP: Adaptations and InnovationsThe Future of PHP: Adaptations and InnovationsApr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

When would you use a trait versus an abstract class or interface in PHP?When would you use a trait versus an abstract class or interface in PHP?Apr 10, 2025 am 09:39 AM

In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

What is a Dependency Injection Container (DIC) and why use one in PHP?What is a Dependency Injection Container (DIC) and why use one in PHP?Apr 10, 2025 am 09:38 AM

Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

Explain the SPL SplFixedArray and its performance characteristics compared to regular PHP arrays.Explain the SPL SplFixedArray and its performance characteristics compared to regular PHP arrays.Apr 10, 2025 am 09:37 AM

SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

How does PHP handle file uploads securely?How does PHP handle file uploads securely?Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

What is the Null Coalescing Operator (??) and Null Coalescing Assignment Operator (??=)?What is the Null Coalescing Operator (??) and Null Coalescing Assignment Operator (??=)?Apr 10, 2025 am 09:33 AM

In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment