我怎么不在PHP中使用全局包含文件

时间:2013-02-24 10:59:05

标签: 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;
 $link = new Links;
 ?>

请解释并参考描述此事的文章。

3 个答案:

答案 0 :(得分:2)

您想要查看PHP的自动加载:http://php.net/manual/en/language.oop5.autoload.php

它将极大地帮助简化您的收录过程!

答案 1 :(得分:1)

使用PHP手册中描述的类autoloading

答案 2 :(得分:1)

您需要使用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);
    }

希望能帮到你。