-2

如何在不使用全局包含文件的情况下简化以下代码中的类包含。

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;
 ?>

请解释并参考描述这一点的文章。

4

3 回答 3

2

你想看看 PHP 的 Autoload: http: //php.net/manual/en/language.oop5.autoload.php

它将极大地帮助简化您的包含过程!

于 2013-02-24T11:06:12.663 回答
1

使用PHP 手册中描述的类的自动加载。

于 2013-02-24T11:06:07.080 回答
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);
    }

希望对您有所帮助。

于 2013-02-24T11:11:27.200 回答