如何在php中不使用全局包含文件
如何简化以下代码中类的包含,而不必 使用全局包含文件如何在php中不使用全局包含文件,php,Php,如何简化以下代码中类的包含,而不必 使用全局包含文件 enter code here <?php include "class/class.Prode.php"; include "class/class.Groupes.php"; include "class/class.Pages.php"; include "class/class.Links.php"; $prod = new Prode; $group = new Groups; $page = new Pages
enter code here
<?php
include "class/class.Prode.php";
include "class/class.Groupes.php";
include "class/class.Pages.php";
include "class/class.Links.php";
$prod = new Prode;
$group = new Groups;
$page = new Pages;
$link = new Links;
?>
在此处输入代码
请解释并参考描述这一点的文章。使用PHP手册中描述的类。您想了解PHP的自动加载:
这将大大简化您的纳入流程 您需要使用PHP的自动加载功能,下面我为您提供了一个示例
function __autoload($class_name) {
$class_name = strtolower($class_name); // you may need to omit this or rename your files
$file = "class.{$class_name}.php";
$directory = "/path/to/your/class/directory/";
if($full_path = recursive_file_exists($file, $directory)) {
require($full_path);
} else {
// check if it exists with an s on the end
// a nice fallback to cover forgetfulness
$file = "class.{$class_name}s.php";
if($full_path = recursive_file_exists($file, $directory)) {
require($full_path);
} else {
die("The file class.{$class_name}.php could not be found in the location ".$directory);
}
希望这对你有所帮助。我相信这是最简单的事情了吧?除非您想要某种类型的递归包含(例如,在类文件夹中包含everyfile),否则您需要一个自动加载器。查看spl_autoload_register()并用谷歌搜索它,还可以查看是否使用了可以包含它的包或库。