包含函数文件的效率(在 PHP 中)

发布于 2024-08-18 05:44:11 字数 410 浏览 4 评论 0原文

如果我有大量函数,最好将它们全部保存在一个大文件中,还是将它们分成多个相关函数的文件。我所说的更好是指在可维护性和服务器处理请求方面都更加高效。

例如,现在我将所有文件都放在名为 include.php 的文件中。但是拥有一个包含以下内容的包含文件会更明智吗:

<?php
 include('/functions/user.php');
 include('/functions/admin.php');
 include('/functions/content.php');
 include('/functions/nav.php');
 include('/functions/database.php');
 include('/functions/other_junk.php');
?>

If I had a large number of functions would it be better to keep them all in one large file or would it be better to separate them into several files of related functions. By better I mean more efficient for both maintainability and for the server processing the request.

For example right now I have all my files in a file named include.php. But would it be wiser to have an include file of includes like:

<?php
 include('/functions/user.php');
 include('/functions/admin.php');
 include('/functions/content.php');
 include('/functions/nav.php');
 include('/functions/database.php');
 include('/functions/other_junk.php');
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

送舟行 2024-08-25 05:44:11

为了可维护性,绝对将它们分开。我怀疑性能会受到影响,但即使会受到影响(只是一点点),你最好还是编写可维护、可读的代码。

Definitely separate them, for maintainability sake. I doubt performance will suffer at all, but even if it does (just a teensy bit) you're better off writing maintainable, readable code.

千と千尋 2024-08-25 05:44:11

您需要确保您使用的是 XCache 或 APC 等 PHP 缓存。然后,您的 PHP 文件应该全部位于内存中,您根本不必担心您的包含文件会击中磁盘。

如果您将志同道合的函数/类分解到它们自己的文件中,我肯定会发现它更容易。

You want to be sure you're using a PHP cache like XCache or APC. Your PHP files should then all be in memory and you shouldn't be worried about your includes hitting the disk at all.

I would definitely find it easier if you broke up like minded functions/classes into their own files.

甜心小果奶 2024-08-25 05:44:11

就可维护性而言,通常最好将函数分成相关的组。 (如上面所示,user.php 将只是与用户相关的函数)。

如果您知道每次需要包含任何文件时都需要所有包含的文件,那么您应该只拥有一个包含所有这些包含的文件。
否则,它就达不到拥有“包罗万象”文件的目的。

In terms of maintainability, it's usually better to separate your functions into related groups. ( like you showed above, user.php would be only the user-related functions ).

You should only have a file that has all of those includes if you know that you'll need all of the included files every time you need to include any file.
Otherwise, it defeats the purpose of having that 'catch-all' file.

尘曦 2024-08-25 05:44:11

根据我的经验,如果您正在谈论函数库的几十个左右文件,那么多个包含和/或要求通常不会给您带来太多麻烦。特别是如果您可以设法在请求生命周期内仅调用特定文件的语句一次。

当您进入 OOP 或高度复杂的函数/过程类型体系结构(其中可能有数百个不同的类/文件)时,它开始显示性能下降。但通常在那时,您将希望通过缓存/编译来完成某种缓解措施。

In my experience multiple includes and/or requires generally arent goping to set you too much back if youre talking a couple dozen or so files for function libraries. Especially if you can manage to only call the statement for a particular file once during a request lifecycle.

Where it starts to show performance hits is if you get into OOP or a highly complex functional/procedural type architecture where there may be hundreds of different classes/files. But generally at that point youll have hopefully done some kind of mitigation via caching/compiling.

酷到爆炸 2024-08-25 05:44:11

我在中央 .config 文件中有一个包含列表。

对于所有 OOP 类,尽管我使用自动加载 ->我知道它会稍微慢一些,但是当我创建新课程时,它省去了将它们包括在内的麻烦。并且它们仅根据需要加载。

顺便说一句, include 比 include_once 更快,因为它不必检查文件是否已包含。

I have a list of includes in a central .config file.

For all OOP classes though I use autoload -> I know it's slightly slower, but it saves having to including them as I make a new class. And they're only loaded as required.

As an aside, include is quicker than include_once as it doesn't have to check if the file has been included already.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文