From d22555583047fc7f6705dc39020cf056cd6cf773 Mon Sep 17 00:00:00 2001 From: Andreas Lutro Date: Fri, 13 Mar 2015 23:54:32 +0100 Subject: [PATCH] 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. --- lib/PhpParser/Autoloader.php | 11 +++++++++++ lib/bootstrap.php | 6 ++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/PhpParser/Autoloader.php b/lib/PhpParser/Autoloader.php index 836f91d..a13ef0f 100644 --- a/lib/PhpParser/Autoloader.php +++ b/lib/PhpParser/Autoloader.php @@ -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; } /** diff --git a/lib/bootstrap.php b/lib/bootstrap.php index 0a62ea6..b0f5178 100644 --- a/lib/bootstrap.php +++ b/lib/bootstrap.php @@ -1,4 +1,6 @@