php - How to register path in autoload using a single instance.

191

Sorry about the vague title, but I am trying to find some better alternatives to having to call anAutoloader class, and theregister method multiple times, to map class paths as seen below.

$ClassLoader = new Autoloader\Loader(__DIR__.'/path/to/someclass');
$ClassLoader->register();

$ClassLoader = new Autoloader\Loader(_DIR__.'/path/to/anotherclass');
$ClassLoader->register();

$ClassLoader = new Autoloader\Loader(__DIR__.'/path/to/anotherclass');
$ClassLoader->register();

$ClassLoader = new Autoloader\Loader(__DIR__.'/path/to/anotherclass');
$ClassLoader->register();

$ClassLoader = new Autoloader\Loader(__DIR__.'/path/to/anotherclass');
$ClassLoader->register();

This goes on and on for about 50 lines, and I would like to know how I can handle the autoloading classes with simple few lines solution. I can obviously inject an array, to the constructor:

 $ClassLoader = new Autoloader\Loader( ['paths'=>[
                     '/path/to/class/', 
                     '/path/to/anotherclass',
                     '/path/to/anotherclass'
 ]);
 $ClassLoader->register();

But, I am not sure if this method is recommended at-least from OOP good practice point of view.

603

Answer

Solution:

Perhaps this is what you are looking for. For each directory that contains your classes run::add.

namespace ClassLoader;

class Loader
{
    protected $directories = array();


    public function __construct()
    {
        spl_autoload_register([$this, 'load']);
    }

    public function add($dir)
    {
        $this->directories[] = rtrim($dir, '/\\');
    }

    private function load($class)
    {
        $classPath = sprintf('%s.php', str_replace('\\', '/', $class));

        foreach($this->directories as $dir) {
            $includePath = sprintf('%s/%s', $dir, $classPath);

            if(file_exists($includePath)) {
                require_once $includePath;
                break;
            }
        }
    }
}

$loader = new Loader();

$loader->add(__DIR__.'/src');
$loader->add(__DIR__.'/vendor');

use Symfony\Component\Finder\Finder;

$finder = new Finder(); 
// Included /var/www/test/vendor/Symfony/Component/Finder/Finder.php
// I put the Symfony components in that directory manually for this example.

print_r($finder);

It is effectively the same as with composer though, just less adaptive or performant.

908

Answer

Solution:

For this you can use Composer: https://getcomposer.org/download/

You will get a file calledcomposer.phar. Place this in your project directory, then go to that directory on your command line.

Runphp composer.phar init.

This will ask you a few questions which you can ignore, in the end you get a new file calledcomposer.json

It should look something like this:

{
    "autoload": {
        "psr-0": { "": "src/" }
    },
    "require": {}
}

Add theautoload field, and replacesrc/ with the directory containing your classes. Make sure that directory exists.

Then runphp composer.phar install.

This will create a directory calledvendor. Inside this directory is a file calledautoload.php.

Include this file in the bootstrap of your project, and all classes within your source directory will automatically be loaded in.

943

Answer

Solution:

Have you looked into spl_autoload_register function?

usage

// pre php 5.3
function my_autoloader($class) {
    include 'classes/' . $class . '.class.php';
}
spl_autoload_register('my_autoloader');

// Or, using an anonymous function as of PHP 5.3.0
spl_autoload_register(function ($class) {
    include 'classes/' . $class . '.class.php';
});

then place all your classes in "classes" folder and when you initialise them with thenew keyword, they will be auto-included. Works with static classes also.

for example:

$myClassOb1 = new MyClass();
// will include this file: classes/MyClass.class.php

or

$email = Utils::formatEmail($emailInput);
// will include this file: classes/Utils.class.php

People are also looking for solutions to the problem: wordpress - Parse error: syntax error, unexpected '<' in /functions.php on line 155

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.