【问题标题】:How to include php file automatically?如何自动包含php文件?
【发布时间】:2018-05-25 18:09:23
【问题描述】:

我在项目的根文件夹中创建了一个 functions.php。我的项目有一些其他文件夹,如 inc,在此文件夹中有 2 个文件夹,如 folder-1folder-2。因此,当我创建一个像 something.php 这样的 php 文件时,我会将它包含到我的 functions.php 文件中,例如 include_once 'inc/folder-1/something.php '。但是当我创建许多 php 文件时,每次我都需要将这些文件包含或要求到我的函数文件中。

有没有办法将所有的php文件动态包含在一起?

就像,我创造..

文件夹 1 中的 something-1.php

文件夹 1 中的某物 2.php

文件夹 1 中的 something-3.php

文件夹2中的nothing-1.php

文件夹2中的nothing-2.php

文件夹2中的nothing-3.php

现在我通过这样的写法将此文件包含到我的函数文件中..

include_once 'inc/folder-1/something-1.php';

include_once 'inc/folder-1/something-2.php';

include_once 'inc/folder-1/something-3.php';

include_once 'inc/folder-2/nothing-1.php';

include_once 'inc/folder-2/nothing-2.php';

include_once 'inc/folder-2/nothing-3.php';

有什么办法可以避免本手册包括系统?

比如,当我在这些文件夹中创建一个新文件时,它会自动包含在 functions.php 文件中,当我删除它时,不会对我的项目产生任何影响...

【问题讨论】:

标签: php include


【解决方案1】:

您可以使用glob

对于文件夹 1

foreach (glob("inc/folder-1/*.php") as $yourfilename)
{
    include_once $yourfilename;
}

对于文件夹 2

foreach (glob("inc/folder-2/*.php") as $yourfilename)
{
    include_once $yourfilename;
}

【讨论】:

  • 这是一个相当基本的方法来解决一个已经通过自动加载解决的问题
【解决方案2】:

首先创建一个类似于“allfiles.php”的文件名,然后将您想要包含的所有文件包含到文件示例中。 include 'inc/folder-1/something.php' 后面是所有文件,直到最后一个 nothing.php,现在保存它,只要你想包含只包括“allfiles.php”所有文件都将被包括希望它有帮助?

【讨论】:

    【解决方案3】:

    您可以在 php.ini 文件中设置 auto_prepend_file 指令,

    http://php.net/manual/en/ini.core.php#ini.auto-prepend-file

    参考:PHP auto include

    在删除之前确保该文件不依赖于它

    【讨论】:

      猜你喜欢
      • 2011-08-15
      • 2012-01-13
      • 1970-01-01
      • 2015-01-23
      • 2011-02-22
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 2015-05-11
      相关资源
      最近更新 更多