I am having a weird issue with the autoloader class I built in PHP to use in WordPress.
The core concept is that it should load class regardless of if you are a child theme, parent theme or a plugin. It should find the class based on the name (coreesponding to the folder structure: CoreTheme_Folder_SubFolder_ClassName(){}
= CoreTheme/Folder/SubFolder/ClassName.php
This works fine in child themes and parent themes, In child themes I include the parent themes function.php and from there it loads the autoloader so that I can call parent theme classes in child themes and not have a bunch of require_once()
every where.
The problem comes, when I am doing the same concept in a plugin. The Autoloader is apart of a framework (think of zend) that you include at the root of your theme. If that theme is active and I set up the autoloader in a plugin, it spazzes out saying "there are two instances of this autoloader, what are you doing?" If I deactivate said theme then instantiate the autoloader in the plugin, it cant find any class I create using the pear naming standards.
So My question to those who understand both WordPress and PHP on a deeper level, How do I create my autoloader such that, regardless of weather your theme, child theme or plugin it will always find the class you are looking for.
the code below is the autloader class. If you take out: plugin_dir_path(__FILE__)
then what will happen is the autoloader will load any class in the theme and corresponding child theme.
class AisisCore_Loader_AutoLoader{
protected static $_instance;
protected static $_directories;
public function get_instance(){
if(null == self::$_instance){
self::$_instance = new self();
}
self::$_directories = array(
get_template_directory(),
get_stylesheet_directory(),
plugin_dir_path(__FILE__),
);
return self::$_instance;
}
public function reset_instance(){
self::$_instance = null;
}
public function register_auto_loader(){
spl_autoload_register(array($this, 'load_class'));
}
public function load_class($class){
$path = str_replace('_', '/', $class);
foreach(self::$_directories as $directories){
if(file_exists($directories . '/' . $path . '.php')){
require_once($directories . '/' . $path . '.php');
}
}
}
}
The class is instantiated in your parent functions.php via:
// Load the autoloader.
require_once(get_template_directory() . '/AisisCore/Loader/AutoLoader.php');
// Setup the autoloader.
$auto_loader = AisisCore_Loader_AutoLoader::get_instance();
$auto_loader->register_auto_loader();
And then in the child theme all you have to do is:
require_once(TEMPLATEPATH . '/functions.php');
This allows the autoloader to work in both child and parent theme. The main issue is that when you create a plugin you place a copy of the framework in your plugin's Name root directory: plugins/myplugin/AisisCore/
and one in your theme (parent) Themes/MyTheme/AisisCore
So I can see why it would freak out - sort of, how ever is there a way around it?