I am trying to use spl_autoload_register and I want to create an if() statement that will check if the class method as already been registered.
For example:
if (spl_autoload_function(array($this, '_loadClass'))
// Then do nothing
else
// Then run spl_autoload_register(array($this, '_loadClass));
Is something like the above code/method possible?
You shouldn’t. There is no need to do the if statement;
spl_autoload_registerwill just ignore a second call with the same arguments:The output shows that although we called
spl_autoload_registertwice, there is still just one autoloader;If you want to make sure a class doesn’t register itself twice, you can of course set that in some kind of static member;
The output is obviously one line:
That said, why are you writing your own autoloader? There are numerous out there already, it’s hard to believe that none of them fit your requirements.