从不同的文件和目录自动加载 类 和函数

Autoload classes and functions from different files and directories

我有这个自动加载代码:

function __autoload($class_name)
{
    //class directories
    $directorys = array(
        './Controls/',
        './Config/',
        './Utility/'
    );
    //for each directory
    foreach($directorys as $directory)
    {
        //see if the file exsists
        if(file_exists($directory.$class_name . '.php'))
        {
            require_once($directory.$class_name . '.php');
            //only require the class once, so quit after to save effort (if you got more, then name them something else
            return;
        }
    }
}

我有三个目录,它们包含我所有的 classes 和函数。

我可以在 Controls 目录中创建一个自动加载文件并使用它来加载其他 php 文件中的所有函数或 classes 我的意思是例如 index.php文件在 /portal/main/index.php

是否可以在 index.php 文件中加载 class controlsconfig 的文件而不包含 index.php 文件上方的任何文件

我的意思是自动加载会自动理解哪个文件正在请求 class 或函数,并为其包含该文件。

更新代码:

function __autoload($class_name)
{
    //class directories
    $directorys = array(
        '/Controls/',
        '/Config/',
        '/Utility/'
    );
    //for each directory

    $ds = "/"; //Directory Seperator
    $dir = dirname(__DIR__); //Get Current file path
    $windir = "\"; //Windows Directory Seperator
    $path = str_replace($windir, $ds, $dir);

    foreach($directorys as $directory)
    {
        //see if the file exsists
        if(file_exists( $path . $directory . $class_name . '.php'))
        {
            require_once( $path . $directory . $class_name . '.php');
            //only require the class once, so quit after to save effort (if you got more, then name them something else
            return;
        }
    }
}

我已经更新了代码,它包含了文件,但我遇到的唯一问题是这个功能不是自动 运行,

例如我的自动加载文件位于:root/Controls/autoload.php 我需要一些 classes 和功能:root/portal/index.php

当我在 index.php 中定义 classes 时,出现文件不存在的错误 我应该在 index.php

中手动调用 autoload.php 文件

我怎样才能使自动加载变得智能,我不应该将它包含在每个文件中以包含 classes ?

请帮帮我。 提前致谢

  1. 你的程序只和你一样聪明。 如果你梦想解决一个主流语言能力之外的问题,那么问题出在你的梦想上,而不是语言。

  2. 要自动加载 PHP 代码,请将 php.ini 中的 auto_prepend_file 设置为您的自动加载程序脚本。 或者,您可以将其作为集中入口点并使用 mod_rewirte.

  3. 直接访问

This is bad practice. Both methods relies on server config such as .htaccess, and is not always available. You may also override existing configs or fail to rewrite some paths.

You should manually centralise the PHP requests (and seal off non-entry points), or require_once in each entry points.

  1. PHP does not支持自动加载功能。 如果您懒得编写包含的函数,请包含您可能需要的所有函数。

Look at Wikipedia, Magento, or WordPress. They have hundreds if not thousands global functions.

Do not worry about including - trust your disk cache and enable opcode cache. Static inclusion is pretty fast and with opcode cache it may be faster than conditional loads.

  1. 如果文件中有多个 类 / 函数,我知道没有任何动态语言可以神奇地选择要包含的正确文件。 你的自动装弹器看起来很漂亮 standard.

You can dynamically define anything in PHP. PHP can never be confident what it will get unless it really runs the code. Auto loaders are simply trusting filenames as correct and complete declaration of what is in the file.

Since your autoloader scans all folders, you may want to make a static file map on first run, and use the map for subsequence loads. Assuming you are not going to create class files dynamically with PHP, of course.

简单的手动解决方案:将您的 autoload 文件放在您的项目根目录中,并将其包含在您的索引文件中,这将完成这项工作。

但如果您想使用 htaccessphp.ini : 将名为 .user.ini 的文件放入文档根目录并在其中添加 auto_prepend_file 指令:

auto_prepend_file = /home/user/domain.com/init.php

文件必须在 PHP 的 include_path 中。因此,您必须将文件的目录设置在 php.ini 内的 include_path 中,或者在 .htaccess 中使用 php_value 语句进行设置。

php_value include_path ".:/path/to/file_directory"
php_value auto_prepend_file "file.php

如果你在 .htaccess 中使用上述方法,请务必从 php.ini 中复制 include_path 并添加 :/path_to/file_directory 这样你就不会丢失任何已经需要的内容包括。

或者,只需添加:/path/to/file_directory to include_path directly in the php.ini

更新

如果您无法修改 include_path,您可以尝试指定 auto_prepend_file 的相对路径。这应该有效,因为发送的文件路径的处理方式与使用 require():

调用时的处理方式相同
php_value auto_prepend_file "./file.php"