简体   繁体   English

PHP的自动加载类目录?

[英]PHP Autoload classes with directories?

This is my current autoLoader: 这是我当前的autoLoader:

function classAutoLoad($class) {
    if (file_exists($_SERVER['DOCUMENT_ROOT']."/framework/includes/class/$class.class.php"))
        include($_SERVER['DOCUMENT_ROOT']."/framework/includes/class/".$class.".class.php");
}

spl_autoload_register('classAutoload');

Usage: 用法:

$class = new Classname;

Basically it will load all classes inside /class/, not including directories. 基本上,它将加载/ class /内的所有类,不包括目录。 What I am trying to do is, be more clean. 我想做的是,变得更加干净。

I have a lot of classes, and I want to package them into directories. 我有很多类,我想将它们打包到目录中。

How can I enable support for packages here? 如何在此处启用对软件包的支持? Or is there a popular library for this? 还是有一个受欢迎的图书馆?

You have to come up with some way of mapping class names to files. 您必须想出一些将类名映射到文件的方法。 You can do this explicitly by maintaining an associative array or your can use some convention like PSR-0 . 您可以通过维护关联数组来明确地执行此操作,也可以使用诸如PSR-0之类的约定。 PSR-0 states that namespaces should translate to directories in the $class that gets passed to your autoloader, so you can replace the namespace sepratator \\ by the directory separator of your system. PSR-0指出,名称空间应转换为传递给自动加载器的$class中的目录,因此您可以用系统的目录分隔符替换名称空间sepratator \\

Your code seems to be better decent for what you're trying to do, loading in one specific directory. 您的代码似乎更适合您要执行的操作,将其加载到一个特定目录中。 However, I would change the if (file_exists(...)) { ... } to trigger an error if the file doesn't exist. 但是,如果文件不存在,我将更改if (file_exists(...)) { ... }以触​​发错误。 Something like 就像是

if (file_exists(...)) {
    ...
} else {
    throw new RuntimeException($class . ' could not be found.');
}

You can take a look at Loader.php which is the autoloader script that I use. 您可以看一下Loader.php ,这是我使用的自动加载器脚本。 This takes into account namespaces and can be modified to only look at *.class.php . 这考虑了名称空间,可以修改为仅查看*.class.php

To use it: 要使用它:

require_once($_SERVER['DOCUMENT_ROOT']."/path/to/Utilities/Loader.php");
\Utilities\Loader::register();

$class = \Namespace\ClassName();

If the autoloader does not have to be fully dynamic, you can simly use PHP Autoload Builder . 如果自动加载器不必完全动态,则可以简单地使用PHP Autoload Builder It scans your entire directory and automatically creates a static class-to-filename mapping that looks roughly like this: 它会扫描您的整个目录,并自动创建一个静态的类到文件名的映射,看起来像这样:

spl_autoload_register(function($class) {
    static $classes = null;
    if ($classes === null) {
        $classes = array(
            'my\\first\\class' => 'My/First/Class.php',
            'my\\secondclass' => 'My/SecondClass.php'
        );
    }
    $cn = strtolower($class);
    if (isset($classes[$cn])) {
        require __DIR__ . $classes[$cn];
    }
});

Pros: 优点:

  • Fast and robust 快速而强大
  • Flexibility (basically, you can use any naming convention you like) 灵活性(基本上,您可以使用任何喜欢的命名约定)

Cons: 缺点:

  • Needs to be re-run every time you add, remove or rename classes. 每次添加,删除或重命名类时都需要重新运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM