Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple autoloader for XHP in Laravel
* Simple autoloader for XHP in Laravel, when you don't want to use Composer's
* autoloader. To use:
* 1. Place this file somewhere in an autoloader-enabled directory of your
* Laravel installation (app/controllers is fine).
* 2. Modify `app/config/app.php` and add 'XHP\XHPServiceProvider' to the
* "Autoloaded Service Providers" section.
* More info on XHP in Laravel:
namespace XHP;
use Illuminate\Support\ServiceProvider;
class XHPServiceProvider extends ServiceProvider {
const CLASS_PREFIX = 'xhp_';
public function register() {
spl_autoload_register(function($className) {
if (static::isXHPClass($className)) {
$filename = static::classToFilename($className);
$path = $this->app['view.finder']->find($filename);
require $path;
return true;
return false;
public static function isXHPClass($className) {
$prefix = substr($className, 0, strlen(static::CLASS_PREFIX));
return $prefix === static::CLASS_PREFIX;
public static function classToFilename($className) {
return str_replace(
array('__', '_'),
array('/', '-'),
substr($className, strlen(static::CLASS_PREFIX))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.