1

I have a problem with loading classes in a "includes" folder. Tried on a few WP installations and always same result.

I've made a simple plugin to see if the problem is somewhere else but again, it fails for some unknown reason.

Plugin folder name test

test.php

<?php
/*
Plugin Name: test
Plugin URI: test
Description: 
Version: 1.0
Author: test
Author URI: test
*/

class test_main {
    public function __construct() {
        include_once( 'includes/autoloader.php' );
    }   
}

new test_main();

Then inside the includes folder I have two files, one of them is autoloader and another water class which for this purpose should kill it if it is loaded.

autoloader.php

<?php
class autoloader {
    public function __construct() {
        spl_autoload_register( array( $this, 'auto_load_classes' ) );
    }

    public function auto_load_classes( $class_name ) {
        if ( is_readable( plugin_dir_path( dirname( __FILE__ ) ) . 'includes/' . $class_name . '.php' ) )
        {
            include_once( plugin_dir_path( dirname( __FILE__ ) ) . 'includes/' . $class_name . '.php' );
        }
    }
}

new autoloader();

water.php

<?php
class water {
    public function __construct() {
        die( 'water' );
    }
}

new water();

water class is never loaded, why?

echo $class_name;

in autoloader.php returns: wp_atom_server WP_User_Search

I went to check a few plugins to see if anyone is using this set up and the only one I could find is WooCommerce and they basically have the same set up as above but for me it fails. Anyone has any kind of an idea?

Edit

Well this is embarrassing and shows that one should sleep more :) new water(); should be called outside of the loader as the loader should not instantiate the class. new water(); should be called in this example case in test.php file after new test_main();. Works just fine then.

  • Can you echo out what the final path is before trying to include it? – Shawn Apr 20 '15 at 22:01
  • Yes, I did echo the path and the path to the folder is correct. Tried and checked this on multiple installations, two different servers. – Getenburg Apr 21 '15 at 8:42
  • The path is: /home/test_site/wp-content/plugins/test/includes/ which is correct. – Getenburg Apr 21 '15 at 8:45
  • I'm glad you figured that out. :-) When the question gives you a chance answer your own question and give it the checkmark so others can find this answer in the future who may have a similar issue. – Shawn Apr 21 '15 at 16:35
  • @Shawn, yes, I just did that but I have to wait until tomorrow to mark is as solved. Thanks for reminding me. – Getenburg Apr 21 '15 at 17:48
1

Right, to answer my own stupidity.

spl_autoload_register is called when you instantiate a class.

Moving new water(); from file water.php to file test.php solves the problem.

The test.php should then look like this:

<?php
/*
Plugin Name: test
Plugin URI: test
Description: 
Version: 1.0
Author: test
Author URI: test
*/

class test_main {
    public function __construct() {
        include_once( 'includes/autoloader.php' );
    }   
}

new test_main();

new water();

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.