Title: Autoloading classes
Last modified: October 14, 2021

---

# Autoloading classes

 *  [janmoes](https://wordpress.org/support/users/janmoes/)
 * (@janmoes)
 * [4 years, 8 months ago](https://wordpress.org/support/topic/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)

 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [4 years, 8 months ago](https://wordpress.org/support/topic/autoloading-classes/#post-14972114)
 * 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](https://wordpress.org/support/users/janmoes/)
 * (@janmoes)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/autoloading-classes/#post-14972781)
 * 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)

The topic ‘Autoloading classes’ is closed to new replies.

## Tags

 * [autoload](https://wordpress.org/support/topic-tag/autoload/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [janmoes](https://wordpress.org/support/users/janmoes/)
 * Last activity: [4 years, 7 months ago](https://wordpress.org/support/topic/autoloading-classes/#post-14972781)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
