php autoloader如何工作 [英] how does php autoloader works

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

问题描述

php类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 Autoloader的工作方式

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.

如果将所有类放在一个文件中,则不需要自动加载器.当然,在对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();

这里发生的是,当创建autoloader类时,类方法Classloader被注册为autoloader.

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.

当然,您可以使它变得非常复杂.让我们看一个带有名称空间和映射器的示例.假设我们在autoloader类中:

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 autoloader如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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