包含不必要的 php 文件会减慢网站速度吗? [英] Will including unnecessary php files slow down website?

查看:28
本文介绍了包含不必要的 php 文件会减慢网站速度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能会促使一些人几乎立即做出明确的是"或否",但请继续阅读...

我有一个简单的网站,其中有 30 个 php 页面(每个页面都有一些 php 服务器端代码 + HTML/CSS 等...).没有复杂的层次结构,什么都没有.只有 30 页.

我还有一组纯粹的后端 php 文件 - 这些文件包含用于将内容保存到数据库、进行身份验证、发送电子邮件、处理订单等的代码.这些将被那 30 个内容页面重复使用.

我有一个主 php 文件,我向其中发送了一个参数.这指定了需要这 30 个文件中的哪一个,并且它包含适当的内容页面.但是每一个都可能需要包含可变数量的后端文件.例如,一个内容页面可能不需要来自后端的任何内容,而另一个可能需要数据库代码,而其他内容页面可能需要电子邮件、数据库和身份验证代码等...

我想无论需要什么后端页面,都可以包含在相应的内容页面中,但是路径中的一个小变化我必须编辑数十个文件.检查哪个内容页面被请求(切换案例类型的东西)并在主php文件中包含适当的后端文件会太麻烦.同样,如果单个路径发生变化,我必须进行许多更改.

由于懒惰,我将所有后端文件都包含在主文件中,这样任何内容页面都无法请求未包含的内容.

第一个问题 - 这是一个好习惯吗?如果它是由任何人完成的.

其次,无论是否需要,都会有性能问题或由于我包括所有后端文件而导致的任何类型的问题吗?

编辑

该网站每天的访问量在 3000 到 4000 之间.

解决方案

你应该进行基准测试.使用不同的包含时间执行同一页面.但我想 30 个文件不会有太大区别.

但您可以节省时间,只需在php.ini(它是一个 PECL 扩展,所以你需要安装).它将缓存文件的解析内容,这将显着加快速度.

顺便说一句:懒惰并没有错,它甚至是一种美德 ;)

The question might prompt some people to say a definitive YES or NO almost immediately, but please read on...

I have a simple website where there are 30 php pages (each has some php server side code + HTML/CSS etc...). No complicated hierarchy, nothing. Just 30 pages.

I also have a set of purely back-end php files - the ones that have code for saving stuff to database, doing authentication, sending emails, processing orders and the like. These will be reused by those 30 content-pages.

I have a master php file to which I send a parameter. This specifies which one of those 30 files is needed and it includes the appropriate content-page. But each one of those may require a variable number of back-end files to be included. For example one content page may require nothing from back-end, while another might need the database code, while something else might need the emailer, database and the authentication code etc...

I guess whatever back-end page is required, can be included in the appropriate content page, but one small change in the path and I have to edit tens of files. It will be too cumbersome to check which content page is requested (switch-case type of thing) and include the appropriate back-end files, in the master php file. Again, I have to make many changes if a single path changes.

Being lazy, I included ALL back-end files inthe master file so that no content page can request something that is not included.

First question - is this a good practice? if it is done by anyone at all.

Second, will there be a performance problem or any kind of problem due to me including all the back-end files regardless of whether they are needed?

EDIT

The website gets anywhere between 3000 - 4000 visits a day.

解决方案

You should benchmark. Time the execution of the same page with different includes. But I guess it won't make much difference with 30 files.

But you can save yourself the time and just enable APC in the php.ini (it is a PECL extension, so you need to install it). It will cache the parsed content of your files, which will speed things up significantly.

BTW: There is nothing wrong with laziness, it's even a virtue ;)

这篇关于包含不必要的 php 文件会减慢网站速度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆