最佳实践:放置所需文件的位置

时间:2022-11-13 22:53:10

I'm working with a number of 'helper' classes, which affectively have a bunch of static functions which allow the controllers and actions have access to chunks of shared functionality.

我正在使用许多“帮助程序”类,它们有效地拥有许多静态函数,这些函数允许控制器和操作访问共享功能块。

Problem is that these files have been used as a dumping ground for any functionality which is required across the modules/application and as a result they are > 3k lines in size and at the top they've got about 50 require_once declarations!

问题是这些文件已经被用作模块/应用程序所需的任何功能的转储基础,因此它们的大小超过3k行,并且在顶部它们有大约50个req​​uire_once声明!

Obviously if a view in the application wan't to use a small part of the functionality available from these helpers it inherits all the required files, and you end up bloating your app.

显然,如果应用程序中的视图不能使用这些帮助程序提供的一小部分功能,那么它将继承所有必需的文件,并最终导致应用程序膨胀。

If I were to include the files on a per need basis, I could end up making numerous require_once calls to the required files, which has it's own overhead (compounded with frequency), when I need use a large amount of the functionality available from these helper files.

如果我按需要包含这些文件,我最终可能会对所需的文件进行大量的require_once调用,当我需要使用这些文件中的大量功能时,这些文件有自己的开销(与频率混合)。帮助文件。

So essentially my question is where is the balance struck and is there a best practice that one can employ?

所以基本上我的问题是平衡在哪里,是否有人可以采用的最佳实践?

Thanks,

Flunga

2 个解决方案

#1


9  

Take a look at the autoloading feature. this will reduce all your includes down to only what is required, when it is required.

看看自动加载功能。这样可以在需要时将所有包含减少到只需要的数量。

#2


1  

Your best bet when constructing such dependencies to stay "acyclic". You can have higher-level functionality "require" the low level functionality it needs to operate, but design it in a way so that things do not point to each other.

构建这样的依赖关系以保持“非循环”时最好的选择。您可以让更高级别的功能“需要”操作所需的低级功能,但是要设计它以使事物不会相互指向。

This way, by breaking it into small enough units you will be able to ensure that when you don't need all the modules, only the minimal number of dependencies get pulled in.

这样,通过将其分成足够小的单位,您将能够确保当您不需要所有模块时,只需要引入最少数量的依赖项。

I'm certainly unaware of any reason to "require" code you're not going to use in a page.

我当然不知道有任何理由“要求”你不会在页面中使用的代码。

#1


9  

Take a look at the autoloading feature. this will reduce all your includes down to only what is required, when it is required.

看看自动加载功能。这样可以在需要时将所有包含减少到只需要的数量。

#2


1  

Your best bet when constructing such dependencies to stay "acyclic". You can have higher-level functionality "require" the low level functionality it needs to operate, but design it in a way so that things do not point to each other.

构建这样的依赖关系以保持“非循环”时最好的选择。您可以让更高级别的功能“需要”操作所需的低级功能,但是要设计它以使事物不会相互指向。

This way, by breaking it into small enough units you will be able to ensure that when you don't need all the modules, only the minimal number of dependencies get pulled in.

这样,通过将其分成足够小的单位,您将能够确保当您不需要所有模块时,只需要引入最少数量的依赖项。

I'm certainly unaware of any reason to "require" code you're not going to use in a page.

我当然不知道有任何理由“要求”你不会在页面中使用的代码。