I want to write a class autoloader to use in a wordpress plugin. This plugin will be installed on multiple sites, and i want to minimize the chance of conflicts with other plugins.
The autoloader will be something like this:
function __autoload($name) {
//some code here
}
My main issue is, what happens if another class also uses a function like this? I think it will be bound to give problems. What would be the best way to avoid something like that?
I am trying to not use namespaces so the code will also work on previous versions of php.
Use some implementation like this one.
Basically this autoloader is registered and has the following features:
Now if you want want to do it in OOP way, just add the autoloader function inside a class. (ie: myAutoloaderClass) and call it from the constructor. then simply add one line inside your functions.php
and add in the constructor
Hope this helps. HR
I recommend using namespaces, and PSR-4. You could simply copy this autoloader example from FIG.
But if you don't want to, you can use an autoloader like this one, that defines an convention for WP classes names, and uses that to find the class file.
So, for example, if you call a 'Main' class, then this autoloader will try to include the class file from path: