1
\$\begingroup\$

I made this class, just to understand what PSR autloader is. I didn't want to download and use it without knowing how it works. So, the one I built is very simple, but I would like a review on how to improve it, and what it lacks to be 100% psr-1 compliant.

Here is the class loader.

namespace ClassLoader; 

class Loader
{
    private $classDir; 
    private $namespace; 
    public $dirSeparatorSymbol = '\\'; 

    public function __construct($namespace, $classDir)
    {
        $this->classDir = $classDir; 
        $this->namespace = $namespace;

    }

    private function load($class)
    {

        $include_path = str_replace($this->dirSeparatorSymbol, DIRECTORY_SEPARATOR, $this->classDir);

        $classFilename = substr($class, strrpos($class, '\\') + 1) . '.php';

        if(file_exists($class = $include_path.$classFilename)){
            require $class;

            return true; 
        }



    }   

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

Also: You can see the constructor's first argument: __construct($namespace, ... has a namespace param. But I don't know how this is used. The class loads any functions just by doing:

$a = new ClassLoader\Loader('unused/param', 'path/to/bar');
$a->register(); 

new bar;  
\$\endgroup\$
  • 2
    \$\begingroup\$ You're using a string as a directory separator. PHP defines a cross-platform directory separator in a predefined constant DIRECTORY_SEPARATOR. So, your public $dirSeparatorSymbol can be completely removed. \$\endgroup\$ – jsanc623 Aug 15 '14 at 16:31
  • \$\begingroup\$ ok, that's one valuable insight. Anything more? @jsanc623 \$\endgroup\$ – robue-a7119895 Aug 15 '14 at 20:52
  • \$\begingroup\$ you $classFilename sthingy could be rewritten using strrchr: $classFilename = substr(strrchr($class,'\\'),1) . '.php'; \$\endgroup\$ – Pinoniq Aug 18 '14 at 12:03
  • \$\begingroup\$ @Pinoniq Thanks. I notice that now. But do you have any inputs as to how it I can make it psr-compliant? \$\endgroup\$ – robue-a7119895 Aug 18 '14 at 15:02
  • \$\begingroup\$ You might also want to look into composer. This way you can handle psr-0, psr-4 and even non psr class loading. \$\endgroup\$ – Pinoniq Aug 18 '14 at 15:10
2
\$\begingroup\$

Well, your class is psr-1 compliant. But i think you mean psr-0.

To have a maximum compliance to the psr-0 autoloader. simply copy paste it...

Your method for instance doesn't work for Class_Name

if you call your autoload with \namespace\ClassName the first \ doesn't get trimmed. This will leed to problems. (php will load /path/to/lib//namespace/ClassName.php)

as mentioned in the comments, the use of the $this->dirSeparatorSymbol is weird. You don't need it. so don't use it.

Edit: 'unused' namespace

Often you will have multiple libraries at different places. Your own will probably reside under ROOT.'/lib/' but other libraries you downloaded should/will be under ROOT.'/vendor/AwesomeLibrary/src/'. to accomodate this. You will register 2 autoloaders:

$personalLoader = new ClassLoader\Loader('Personal','/lib/'); $personalLoader->register();

$awesomeLoader = new ClassLoader\Loader('AwesomeLibrary','/vendor/AwesomeLibrary/src/'); $awesomeLoader->register();

Now your autoloader should check or it is allowed to load the file. For instance if I use AwesomeLibrary/MyClass; the personal loader shouldn't do anything since it knows nothing about AwesomeLibrary. the second awesomeLoader however knows how to load the AwesomeLibrary.

\$\endgroup\$
  • \$\begingroup\$ Thanks for finally helping out. Assuming PSR-1 is 'advanced' than PSR-0 I have no reason to make it PSR-0 compliant if, it already fits PSR-1 standards. The only reason, I asked for PSR-0, is because I wanted to start from easier version. So, assume I am talking about PSR-1, I still don't understand what to make of the first argument for the Loader() class. It seems, that is reserved for entering the namespace, but the class already loads namespaced classes without the use of this first argument. So, what I am doing wrong? In simple terms, how should I make use of the first argument? \$\endgroup\$ – robue-a7119895 Aug 18 '14 at 15:19
  • \$\begingroup\$ psr-1 is about coding standards. psr-0 is about autoloading. the advanced autoloading is psr-4 ... \$\endgroup\$ – Pinoniq Aug 18 '14 at 15:48
  • \$\begingroup\$ I guess that is the problem. I thought all was about autoloading. I tried to look at the outoloader, but for me they were a little complicated. Which is why I wanted to make mine and compare it just to see if I have what it takes to build my own autoloader \$\endgroup\$ – robue-a7119895 Aug 18 '14 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.