php 自动加载器是如何工作的 [英] how does php autoloader works

查看:26
本文介绍了php 自动加载器是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

php class Autoloader 是否会打开文件并检查类名?我一直在研究它是如何实际实施的.我知道一件事是递归的?如果我错了请告诉我

Does php class Autoloader opens a file and checks for the class name? I have been looking on how is it actually implemented. One thing I know that its recursive? If I'm wrong please let me know

正如这里提到的:自动加载器简要概述PHP 自动加载器的工作原理

As mentioned overhere : autoloader brief over view How PHP Autoloader works

PHP 自动加载器在定义的目录中递归搜索类、特征和接口定义.没有任何进一步配置所需文件所在的目录用作默认类路径.

The PHP Autoloader searches recursively in defined directories for class, trait and interface definitions. Without any further configuration the directory in which the requiring file resides will be used as default class path.

文件名不需要遵守任何约定.搜索所有文件用于类定义.与类名相似的文件或以 .php 或 .inc 结尾是首选.如果支持,PHP Tokenizer 将用于可靠的类定义发现.

File names don't need to obey any convention. All files are searched for class definitions. Files which are similar to the class name or end with .php or .inc are preferred. If supported, PHP Tokenizer will be used for reliable class definition discovery.

推荐答案

PHP 自动加载器只是一种在构造包含文件的机制.

The PHP autoloader is just a mechanism to include a file when a class is constructed.

如果您将所有类放在 1 个文件中,则不需要自动加载器.当然,在为 OO 编程时,您为每个类提供了自己的文件,这就是自动加载器的用武之地.

If you put all your classes in 1 file, you dont need an autoloader. Of course, when programming OO you give every class its own file, and that's where the autoloader comes in.

一些例子:

class AutoLoader
{  
  public function __construct()
  {
    spl_autoload_register( array( $this, 'ClassLoader' ));
  }

  public function ClassLoader( $class )
  {    
    if( class_exists( $class, false ))
      return true;

    if( is_readable( 'path_to_my_classes/' . $class . '.php' ))
          include_once 'path_to_my_classes/' . $class . '.php'
  }
}

$autoloader = new AutoLoader();

这里发生的事情是,在创建自动加载器类时,类方法 Classloader 被注册为自动加载器.

What happens here is that when the autoloader class is created, the class method Classloader is registered as a autoloader.

创建新类时,Classloader 方法首先检查该类的文件是否已加载.如果不是,则该类前面带有路径并使用扩展名进行扩展.如果文件可读,则将其包含在内.

When a new class is created, the Classloader method first checks if the file for the class is already loaded. If not, the class is prepended with a path and extended with an extension. If the file is readable, it is included.

当然,您可以将其变得非常复杂.让我们看一个带有命名空间和映射器的示例.假设我们在自动加载器类中:

Of course, you can make this very sophisticated. Let's look at an example with namespaces and a mapper. Assume we are in the autoloader class:

  private $mapper array( 'Foo' => 'path_to_foo_files/', 'Bar' => 'path_to_bar_files/');

  public function ClassLoader( $class )
  {    
    if( class_exists( $class, false ))
      return true;

    // break into single namespace and class name
    $classparts = explode( '\', $class ); 
    $path = $this->mapper[$classparts[0]];

    if( is_readable( $path . $classparts[1] . '.php' ))
          include_once $path . $classparts[1] . '.php'
  }

在这里,类名分为命名空间部分和类名部分.在映射器数组中查找命名空间部分,然后将该路径用作 php 文件的包含路径.

Here, the classname is split in the namespace part and the classname parts. The namespace part is looked up in a mapper array and that path is then used as include path for the php file.

这些只是演示自动加载器可以做什么的示例.对于生产,还有一些工作要做,例如错误检查.

These are just examples to demonstrate what can be done with autoloader. For production there is some more work to be done, error checking for example.

这篇关于php 自动加载器是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆