# Simple autoloader, but trying to make it psr-compliat

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.

namespace ClassLoader;

{
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;

• 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. – jsanc623 Aug 15 '14 at 16:31 • ok, that's one valuable insight. Anything more? @jsanc623 – robue-a7119895 Aug 15 '14 at 20:52 • you$classFilename sthingy could be rewritten using strrchr: $classFilename = substr(strrchr($class,'\\'),1) . '.php'; – Pinoniq Aug 18 '14 at 12:03
• @Pinoniq Thanks. I notice that now. But do you have any inputs as to how it I can make it psr-compliant? – robue-a7119895 Aug 18 '14 at 15:02
• You might also want to look into composer. This way you can handle psr-0, psr-4 and even non psr class loading. – Pinoniq Aug 18 '14 at 15:10

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.

• 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? – robue-a7119895 Aug 18 '14 at 15:19