Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

loader - how does php autoloader works

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

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

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.

Some examples:

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();

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

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'
  }

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...