1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2025-01-18 19:41:45 +01:00

Ensure compatibility with multiple autoloaders

Running a .phar or regular PHP executable that requires and includes its own
version of php-parser will cause a "cannot redeclare class" error if said
executable also includes the autoloader of the current working directory.
This commit is contained in:
Andreas Lutro 2015-03-13 23:54:32 +01:00
parent 251e689283
commit d225555830
2 changed files with 15 additions and 2 deletions

View File

@ -7,14 +7,25 @@ namespace PhpParser;
*/
class Autoloader
{
/**
* Whether the autoloader has been registered.
*
* @var boolean
*/
protected static $registered = false;
/**
* Registers PhpParser\Autoloader as an SPL autoloader.
*
* @param bool $prepend Whether to prepend the autoloader instead of appending
*/
static public function register($prepend = false) {
if (static::$registered === true) {
return;
}
ini_set('unserialize_callback_func', 'spl_autoload_call');
spl_autoload_register(array(__CLASS__, 'autoload'), true, $prepend);
static::$registered = true;
}
/**

View File

@ -1,4 +1,6 @@
<?php
require __DIR__ . '/PhpParser/Autoloader.php';
PhpParser\Autoloader::register();
if (!class_exists('PhpParser\Autoloader')) {
require __DIR__ . '/PhpParser/Autoloader.php';
}
PhpParser\Autoloader::register();