问题
To you gurus out there, is there any hidden gem in PHP that could unload a specific extension at runtime ?
回答1:
No, that's not possible and most likely never will:
[2011-02-08 11:34 UTC] rasmus@php.net
extension unloading on a per-request basis simply isn't feasible from a performance point of view. And you obviously can't unload and leave it unloaded for the next request because that next request may be for a page that expects the extension to be there.
However, using dl()
is discouraged anyway - and in recent versions it is only available in the CLI version.
回答2:
From another perspective: It's not possible to remove an extension from a running PHP interpreter, as it may have modified the state of the PHP interpreter in an irreversible fashion. For instance, many extensions register classes when loaded; there's no code in these modules to deregister these classes when unloaded. Worse yet, if your script is already running, it may already contain instances of these classes, which would crash the interpreter if manipulated with the class definition gone.
回答3:
As for workaround, if you are using PHP CLI or its built-in server (php -S
), you can always specify -n
/--no-php-ini
to ignore your php.ini
, so it'll unload all your extension at runtime. Useful for testing.
来源:https://stackoverflow.com/questions/10715925/unloading-php-extensions-reverse-dl