包含我需要的 php 文件中的所有函数还是仅包含我需要的函数?
这就是我想做的。
第一个选项是写每个 每个函数在不同的 php 文件中 一个,然后将它们全部包含在 一个名为 include 的 php 文件 function.php 以及每当我创建时 一个新页面,比方说index.php I 只需包含“function.php”;
为什么我需要这样做?因为我只需要包含一个文件,所有的函数都会包含在内。现在问题可能是服务器负载。我不确定有多少未调用的函数会影响性能。
第二个选项是再次创建我需要的文件,将它们组合起来,然后每当我需要一个函数时就调用它。这样做的缺点是,我需要做更多的工作来进行分类,并且我必须包含大量文件
所以我想问,第一个选项是否会增加我所拥有的CPU和内存负载去第二个吗?第一种方式是否存在任何性能问题,或者 php 根本不解析未使用的函数?
So here is what I want to do.
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
磁盘是服务器中最慢的部分,因此在这种情况下,“所有功能都在 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.
现在是记住Donald Knuth 的名言的时候了:
一般来说,您的开发模型应该调整以匹配项目的需求和目标。在实现目标之后,您可以随时返回到您提出的问题。当您这样做时 您的问题可能会自行解决。
如果您的项目适用的话, 您可能会考虑使用面向对象编程(OOP)。如果你的对象处理它们自己的依赖关系,甚至不再是问题 加载中。
This is a time to remember Donald Knuth's famous quote:
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.