class \mpf\base\AutoLoader

class \mpf\base\AutoLoader

Inheritance \mpf\base\Object
Implements -
Uses \mpf\interfaces\AutoLoaderInterface
Childs -
Available since version  0.1
Source Code

Class AutoLoader

An example of a general-purpose implementation that includes the optional functionality of allowing multiple base directories for a single namespace prefix.

Given a foo-bar package of classes in the file system at the following paths ...

            Baz.php             # Foo\Bar\Baz
                Quux.php        # Foo\Bar\Qux\Quux
            BazTest.php         # Foo\Bar\BazTest
                QuuxTest.php    # Foo\Bar\Qux\QuuxTest
... add the path to the class files for the \Foo\Bar\ namespace prefix as follows:

     // instantiate the loader
     $loader = new \Example\Psr4AutoloaderClass;

     // register the autoloader

     // register the base directories for the namespace prefix
     $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/src');
     $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/tests');

The following line would cause the autoloader to attempt to load the \Foo\Bar\Qux\Quux class from /path/to/packages/foo-bar/src/Qux/Quux.php:

     new \Foo\Bar\Qux\Quux;

The following line would cause the autoloader to attempt to load the \Foo\Bar\Qux\QuuxTest class from /path/to/packages/foo-bar/tests/Qux/QuuxTest.php:

     new \Foo\Bar\Qux\QuuxTest;

This class will only be used if case that the framework is installed without the composer. In case that composer is used to maintain packages that it will also take care of the autoload for all classes.

private static \mpf\base\AutoLoader $lastRegistered

Keeps a link to last instantiated AutoLoader class.

public string[] $prefixes = []

An associative array where the key is a namespace prefix and the value is an array of base directories for classes in that namespace.

public bool $fileExists = false

Will check if file exists or not before including it. For a faster execution this can be set to false but a notice will be generated if file doesn't exists;

private static \mpf\base\AutoLoader[] $_self = []

Keep a link too multiple instances of this class, one instance for each configuration that was called.


-no description found-


public void addNamespace (string $prefix, string $base_dir, bool $prepend = false)

  • $prefix

    The namespace prefix.

  • $base_dir

    A base directory for class files in the namespace.

  • $prepend

    If true, prepend the base directory to the stack instead of appending it; this causes it to be searched first rather than last.

Adds a base directory for a namespace prefix.

public string load (string $name)

  • $name

    Name of class + namespace;

Will include file required for class name;

public string path (string $name, bool $folder = false)

  • $name

    Name of class + namespace;

  • $folder

    If is set as folder .php extension is not added

This method will the path for a class name. Will return the partial path from libs folder;

protected mixed loadMappedFile (string $prefix, string $relative_class, bool $folder)

  • $prefix

    The namespace prefix.

  • $relative_class

    The relative class name.

  • $folder

    If is a folder '.php' won't be added.

Load the mapped file for a namespace prefix and relative class.

protected bool requireFile (string $file)

  • $file

    The file to require.

If a file exists, require it from the file system.

public static \mpf\base\AutoLoader get (string[] $config = [])

  • $config

    Extra config options that can be specified for each class

This method is used to get an instance of this class from anywhere without having to initialize the class each time. It will offer different instances for each config.

public static \mpf\base\AutoLoader getLastRegistered ()

Get last registered autoloader. In most of the cases only one will be registered.

protected mixed init (string[] $config)

  • $config

    Config values that were used when the object was instantiated.

When initiating set value for self::$_self; so that it can be called from ::get(); Multiple values will be set for that, one for each config variant that is initiated.

public mixed register ()

Register this instance as a autoloader; Multiple autoload classes can be registered and somepackages will register their own autoload class. Is better to not remove this class except if you're using composer to handle the autoload process.