【问题标题】:do you think i should keep all my php functions in one file?你认为我应该把我所有的 php 函数放在一个文件中吗?
【发布时间】:2011-04-14 00:21:54
【问题描述】:

我想知道你认为我应该将所有函数保存在一个文件中,还是将它们分开在不同的文件中!

p.s 如果我把所有的函数都放在一个文件里,php 处理这些东西会不会更容易!!

【问题讨论】:

    标签: php function include


    【解决方案1】:

    这取决于它们有多少功能,它们有多长,以及它们做什么。对于任何重要的项目,将所有内容放在一个文件中通常不是一个好主意。他们应该按照他们的工作进行分类。

    将内容保存在单独的文件中不仅对组织和易读性很重要,而且在源代码控制期间查看某个文件已更改但所有其他文件保持不变也确实很有帮助。

    【讨论】:

      【解决方案2】:

      根据我的经验,包含的文件越少,PHP 脚本运行的速度就越快。如果将函数分隔在多个文件中意味着您需要多次使用 include 或 require,则最好将这些函数保存在一个文件中。

      【讨论】:

        【解决方案3】:

        这是过早的优化尝试。处理实际文件内容的时间将超过打开一个关闭文件所节省的时间。因此,您可以通过拆分文件节省 0.01% 的时间。

        【讨论】:

          【解决方案4】:

          对于一个非常大的项目,速度上的损失将被模块化的增益所弥补,如果做得正确,可扩展性。这个函数很简单,很小,可以用来包含任何php然后执行,不需要很长的if() else或者switch case。现在,这可能比 switch case 语句更密集,但对于大型项目,这个功能是完美的。

          function trnFeature_getFeature($feature = 'default', $options = array()) {
              
              $path = __DIR__ . "/features/{$feature}/{$feature}";
              
              //Check the path, if no file exists bail out
              if(!file_exists($path . '.php')) {
                return false;
              }
            
              //The path checked out, include the file
              include_once $path . '.php';
              
              //setup the function that will execute the feature
              $feature_fn = "trnFeature_" . $feature . "_featureInit";
              
              //execute the function, passing it the $options array with all available options
              if(function_exists($feature_fn)) {
                $output = $feature_fn($options);
              } else {
                //you haven't created the correct function yet, so bail out
                return false;
              }
              
              return $output;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-03
            • 2023-03-18
            • 2011-11-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多