Problem with autoloader and namespaces

I’m writing a simple plugin with oop and using namespaces and autoloading. Here is my main file:

add_action(‘plugins_loaded’, array(My_Test_Plugin::get_instance(), ‘plugin_setup’));

class My_Test_Plugin
{
/**
* Plugin instance.
*
* @see get_instance()
* @type object
*/
protected static $instance = NULL;
/**
* URL to this plugin’s directory.
*
* @type string
*/
public $plugin_url = ”;
/**
* Path to this plugin’s directory.
*
* @type string
*/
public $plugin_path = ”;

/**
* Access this plugin’s working instance
*
* @wp-hook plugins_loaded
* @since 2012.09.13
* @return object of this class
*/
public static function get_instance()
{
NULL === self::$instance and self::$instance = new self;
return self::$instance;
}

/**
* Used for regular plugin work.
*
* @wp-hook plugins_loaded
* @return void
*/
public function plugin_setup()
{
$this->plugin_url = plugins_url(‘/’, __FILE__);
$this->plugin_path = plugin_dir_path(__FILE__);
$this->load_language(‘vt-plugin’);

spl_autoload_register(array($this, ‘autoload’));

}
/**
* @param $class
*
*/
public function autoload($class)
{
$class = str_replace(”, DIRECTORY_SEPARATOR, $class);

if (!class_exists($class)) {
$class_full_path = $this->plugin_path . ‘includes/’ . $class . ‘.php’;
if (file_exists($class_full_path)) {
require $class_full_path;
}
}
}
}

In includes/PostTypes directory there is a file called Book.php and it’s containing this:

namespace Book;

class Book {
public function vt_create_book_post_type() {
$labels = array(
‘name’ => _x( ‘Books’, ‘vt-plugin’ ),
‘singular_name’ => _x( ‘Book’, ‘vt-plugin’ ),
‘menu_name’ => _x( ‘Books’, ‘vt-plugin’ ),
),
);

register_post_type( ‘book’, $args );
}
}

It just registers a simple post type. Now my question is Where I should use my init action to register post type?

I used this code in the plugin_setup function in my main file:

add_action( ‘init’, array(‘Book’, ‘vt_register_book_post_type’) );

But it gave me this error:

Warning: call_user_func_array() expects parameter 1 to be a valid callback

Read more here:: Problem with autoloader and namespaces

Leave a Reply

Your email address will not be published. Required fields are marked *