【发布时间】:2018-05-25 18:09:23
【问题描述】:
我在项目的根文件夹中创建了一个 functions.php。我的项目有一些其他文件夹,如 inc,在此文件夹中有 2 个文件夹,如 folder-1 和 folder-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 文件中,当我删除它时,不会对我的项目产生任何影响...
【问题讨论】:
-
Composer 实现了几种常见的自动加载标准。 getcomposer.org/doc/01-basic-usage.md#autoloading
-
不确定你的建筑是最好的结构,但你应该看看:php.net/manual/en/language.oop5.autoload.php