0

我开始关注 PHP 自动加载器。如果我有以下代码:

  function __autoload($class) {require_once('scripts/classes/' . $class . '.class.php');}

只要我的类被命名为适合上面的路径,我是否仍然需要使用 require_once

  require_once('scripts/classes/session.class.php');

注意:我在网站的 header() 中包含了 __autoloading(加载的第一页。我是否需要在加载的每个页面上都包含它才能使其功能正常工作?我会假设是的,但我是不确定...

谢谢你

4

3 回答 3

2

您需要在浏览器中加载的每个页面上定义__autoload或调用。spl_autoload_register因此,如果您的所有页面都使用相同的“标题”,则将其放在那里就足够了。

一旦你这样做了,你永远不需要使用includerequire其他任何地方:只需提及 class Session,如果 PHP 还不知道它,它将使用你的 autoload 函数来查找定义。

于 2013-01-27T03:09:53.127 回答
1

不,您应该使用include(). 该类在尚未加载时仅包含一次:

来自 PHP 手册:

您可以定义一个自动调用的 __autoload() 函数,以防您尝试使用尚未定义的类/接口。

不,您可以在整个应用程序中只包含一次自动加载器功能。

使用spl_autoload_register()而不是__autoload().

于 2013-01-27T02:50:06.660 回答
0

您可以将此文件包含在脚本的开头,然后只需设置目录级别而不是此常量:WP_CONTENT_DIR 这将自动包含它需要的任何文件

<?php

class autoload
{

    private static function updatePhpFiles($dir_level, $php_files_json_name)
    {
        /**Get all files and directory using iterator.*/
        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir_level));
        $filePaths = array();

        /**Get all php files in this directory level.*/
        foreach ($iterator as $path) {
            if (is_string(strval($path)) and pathinfo($path, PATHINFO_EXTENSION) == 'php') {
                $filePaths[] = strval($path);
            }
        }

        /**Encode and save php files dir in a local json file */
        $fileOpen = fopen($dir_level . DIRECTORY_SEPARATOR . $php_files_json_name, 'w');
        fwrite($fileOpen, json_encode($filePaths));
        fclose($fileOpen);
    }

    private static function includeMatchingFiles($dir_level, $php_files_json_name, $class_file_name)
    {
        $files = json_decode(file_get_contents($dir_level . DIRECTORY_SEPARATOR . $php_files_json_name), true);
        $inc_is_done = false;

        /**Include matching files here.*/
        foreach ($files as $path) {
            if (stripos($path, $class_file_name)) {
                require_once $path;
                $inc_is_done = true;
            }
        }
        return $inc_is_done;
    }

    public static function include_system_files($dir_level, $class_name)
    {
        $php_files_json = 'phpfiles.json';
        $class_file_name = $class_name . '.php';

        /**Include required php files.*/
        if (is_file($dir_level . DIRECTORY_SEPARATOR . $php_files_json)) {

            if (self::includeMatchingFiles($dir_level,$php_files_json, $class_file_name)) {
                return true;
            } else {
                self::updatePhpFiles($dir_level, $php_files_json);
                return self::includeMatchingFiles($dir_level, $php_files_json, $class_file_name);
            }

        } else {

            self::updatePhpFiles($dir_level, $php_files_json);
            return self::includeMatchingFiles($dir_level, $php_files_json, $class_file_name);

        }

    }

}

/**
 * Register autoloader.
 */
spl_autoload_register(function ($className) {
    autoload::include_system_files(WP_CONTENT_DIR, $className);
});
于 2018-07-21T08:40:11.110 回答