Skip to content

Instantly share code, notes, and snippets.

@iCaspar
Last active September 18, 2015 12:49
Show Gist options
  • Save iCaspar/539e620a242ae55ded91 to your computer and use it in GitHub Desktop.
Save iCaspar/539e620a242ae55ded91 to your computer and use it in GitHub Desktop.
Class Autoloader for WordPress theme
<?php namespace Potsdam\Inc;
/**
* Potsdam Theme Autoloader
*
* @package Potsdam
* @since 1.0
*/
/** No Direct Access */
if ( ! defined( 'ABSPATH' ) )
die( 'Fizzle' );
/**
* Class autoloader
* @param string $class fully qualified class name
* @return null
*/
function autoloader( $class ) {
$namespace_parts = explode( '\\', $class );
$file_parts = array_map( __NAMESPACE__ . '\\convert_namespace_part_to_file_part', $namespace_parts);
$file = get_theme_root() . '/' . implode( DIRECTORY_SEPARATOR, $file_parts ) . '.php';
if ( file_exists( $file ) ) {
require_once $file;
}
}
spl_autoload_register( __NAMESPACE__ . '\\autoloader' );
/**
* Convert namespace part to file part
* @param string $part namespace part
* @return string filename part
*/
function convert_namespace_part_to_file_part( $part ) {
return str_replace( '_', '-', strtolower( $part ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment