包含我需要的 php 文件中的所有函数还是仅包含我需要的函数?

发布于 2024-11-17 20:03:56 字数 419 浏览 3 评论 0原文

这就是我想做的。

  1. 第一个选项是写每个 每个函数在不同的 php 文件中 一个,然后将它们全部包含在 一个名为 include 的 php 文件 function.php 以及每当我创建时 一个新页面,比方说index.php I 只需包含“function.php”;

    为什么我需要这样做?因为我只需要包含一个文件,所有的函数都会包含在内。现在问题可能是服务器负载。我不确定有多少未调用的函数会影响性能。

  2. 第二个选项是再次创建我需要的文件,将它们组合起来,然后每当我需要一个函数时就调用它。这样做的缺点是,我需要做更多的工作来进行分类,并且我必须包含大量文件

所以我想问,第一个选项是否会增加我所拥有的CPU和内存负载去第二个吗?第一种方式是否存在任何性能问题,或者 php 根本不解析未使用的函数?

So here is what I want to do.

  1. The first option is to write each
    function in different php file each
    one and then include all of them in
    a php file that is called include
    functions.php and whenever I create
    a new page , let's say index.php I
    just include "functions.php";

    Why do I need to do that? Because I'll just have to include only one file and all the functions will be included. Now the problem probably will be the server load. I'm not sure how much uncalled functions affect the performance.

  2. The second option is to create again the files I need, team them up and then whenever I need a function just call it. The drawback of this is that I'll have more work to do in order to categorize and I'll have to include a lot of files

So I want to ask, does the first option increase the cpu and memory load that much that I have to go to the second one? Are there any performance issues with the first way or the functions that are not being used are not parsed at all by the php ?

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

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

发布评论

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

评论(2

倥絔 2024-11-24 20:03:56

磁盘是服务器中最慢的部分,因此在这种情况下,“所有功能都在 1 个文件中”的变体理论上不会给您带来更多的性能。

但我不建议您创建“functions.php”,更好的方法是 OOP。使用方法创建(对象),使用自动加载器和 PSR-0 标准,您会忘记“包括”和“要求”。

Disk is a slowest part of the server, so in this case variant "all functions in 1 file" will give you little more performance, theoretically.

But I don't recommend you to create "functions.php", better way is OOP. Create classes (objects) with methods, use autoloaders and PSR-0 standard and you will forget about "include" and "require" at all.

別甾虛僞 2024-11-24 20:03:56

现在是记住Donald Knuth 的名言的时候了:

程序员浪费了大量的
有时间思考或担心
关于,非关键部分的速度
他们的计划以及这些尝试
在效率上其实有很强的
调试时的负面影响
考虑维护。我们应该
忘记小的效率,比如说
大约 97% 的时间:过早
优化是万恶之源。

然而我们不应该放弃我们的
这关键的 3% 中存在机会。”

一般来说,您的开发模型应该调整以匹配项目的需求和目标。在实现目标之后,您可以随时返回到您提出的问题。当您这样做时 您的问题可能会自行解决。

如果您的项目适用的话, 您可能会考虑使用面向对象编程(OOP)。如果你的对象处理它们自己的依赖关系,甚至不再是问题 加载中。

This is a time to remember Donald Knuth's famous quote:

Programmers waste enormous amounts of
time thinking about, or worrying
about, the speed of noncritical parts
of their programs, and these attempts
at efficiency actually have a strong
negative impact when debugging and
maintenance are considered. We should
forget about small efficiencies, say
about 97% of the time: premature
optimization is the root of all evil.

Yet we should not pass up our
opportunities in that critical 3%."

In general, your development model should be tuned to match the needs and goals of the project. After you have met the goals, you can always return to such questions as the one you asked. When you do that, your question will probably answer itself. The program structure will dictate the best way to handle your includes.

You may wish to consider using object-oriented programming (OOP) if it is applicable to your project. Whenyou use OOP, this problem may even become a non issue if your objects handle their own dependency loading.

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