网站架构 - 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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

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 and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
