Class autoloader in wordpress plugin

*爱你&永不变心* 提交于 2019-12-23 04:56:17

问题


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.


回答1:


Use some implementation like this one.

function TR_Autoloader($className)
{
    $assetList = array(
        get_stylesheet_directory() . '/vendor/log4php/Logger.php',
        // added to fix woocommerce wp_email class not found issue
        WP_PLUGIN_DIR . '/woocommerce/includes/libraries/class-emogrifier.php'
        // add more paths if needed.
    );

// normalized classes first.
    $path = get_stylesheet_directory() . '/classes/class-';
    $fullPath = $path . $className . '.php';

    if (file_exists($fullPath)) {
        include_once $fullPath;
    }

    if (class_exists($className)) {
        return;
    } else {  // read the rest of the asset locations.
        foreach ($assetList as $currentAsset) {
            if (is_dir($currentAsset)) {
               foreach (new DirectoryIterator($currentAsset) as $currentFile) 
{
                    if (!($currentFile->isDot() || ($currentFile->getExtension() <> "php")))
                        require_once $currentAsset . $currentFile->getFilename();
                }
            } elseif (is_file($currentAsset)) {
                require_once $currentAsset;
            }

        }
    }
}

spl_autoload_register('TR_Autoloader');

Basically this autoloader is registered and has the following features:

  • You can add a specific class file, if not following a specific pattern for the location of the files containing your classes (assetList) .
  • You can add entire directories to your search for your classes.
  • If the class is already defined, you can add more logic to deal with it.
  • You can use conditional class definition in your code and then override your class definition in the class loader.

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

new myAutoloaderClass(); 

and add in the constructor

function __construct{
   spl_autoload_register('TR_Autoloader' , array($this,'TR_Autoloader'));
}

Hope this helps. HR




回答2:


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:

<plugin-path>/class-main.php


来源:https://stackoverflow.com/questions/32288861/class-autoloader-in-wordpress-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!