Support » Developing with WordPress » Autoloading classes

  • Hi there, I’ve included classes.php in my core plugin file.
    In classes.php I try to autoload all my classes. When I activate the plugin it should autoload them, but instead I’m getting an error that it tries to autoload a theme .class.php, which isn’t even in my classes folder.

    spl_autoload_register( function($classname) {
    
        $class      = str_replace( '\\', DIRECTORY_SEPARATOR, strtolower($classname) ); 
        $classpath  = plugin_dir_path(__FILE__) .  DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $class . '.php';
        
        if ( file_exists( $classpath) ) {
            include_once $classpath;
        }
       
    });
Viewing 2 replies - 1 through 2 (of 2 total)
  • You should do some basic debugging. First, check what the value of $classpath actually is.

    echo $classpath;

    It sounds like that’s not correct so you should go back into your code to find out where it’s gone wrong.

    Thread Starter janmoes

    (@janmoes)

    Yeah my dumb fault, it was a long day and I was tired. I literally forgot a ‘.’ in front of 'class.php' to get classes like location.class.php

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.