0

First of all thank You for Your time. I use autoload in my 2 plugins. They have to different namespaces but i want to build one main plugin with autoload for this two plugins. In future i am going to add more plugins. How should i organize that? in plugins i have to namespaces

namespace plugin_namespace\controller\some_cntr and

namespace sec_plugin_namespace\controller\some_cntr

everything is object oriented and here is my autoloader witch exists in both plugins (i will have only one if it is possible :))

 /**
 * Register autoloader
 */
public function autoloader(){
    spl_autoload_register( array( $this, 'xx_my_plugin' ) );
}

/**
 * Autoloader
 *
 * @param $class
 */
public function xx_my_plugin( $class ) {
    $first = strpos($class, '\\') + 1;
    $path = substr( $class, $first);
    $path = strtolower( $path );
    $path = str_replace( '_', '-', $path );
    $path = str_replace( '\\', DIRECTORY_SEPARATOR, $path ) . '.php';
    $path = __DIR__ . DIRECTORY_SEPARATOR . $path;

    if ( file_exists( $path ) ) {
        include $path;
    }
}
0

Ok. Problem solved. I added plugin as i said to manage autoloader and other stuff. I changed my autoloader and thanks to that i have one in place of 3. Solution was to change one piece of directory with plugin name.This is my autoloader now:

public function xx_main_plugin( $class ) {

    $array = explode('\\', $class);
    $plugin = str_replace('_', '-', $array[0]);
    $dir =  str_replace( 'in-this-plugin-is-autoloader', $plugin, __DIR__);

    $first = strpos($class, '\\') + 1;
    $path = substr( $class, $first);
    $path = strtolower( $path );
    $path = str_replace( '_', '-', $path );
    $path = str_replace( '\\', DIRECTORY_SEPARATOR, $path ) . '.php';
    $path = $dir . DIRECTORY_SEPARATOR . $path;

    if ( file_exists( $path ) ) {
        include $path;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.