To you gurus out there, is there any hidden gem in PHP that could unload a specific extension at runtime ?
-
What possible reason would you have for this? Just because an extension is loaded, doesn't mean you have to use it. If for some reason you absolutely need to remove a function, you could do it with [runkit](http://www.php.net/manual/en/function.runkit-function-remove.php) although I suspect `if (!function_exists()) { // ... }` or simply disabling it in php.ini with [`disable_functions`](http://www.php.net/manual/en/ini.core.php#ini.disable-functions) would be more appropriate. – DaveRandom May 23 '12 at 08:16
-
@DaveRandom : originated from HttpResponse classname collision between pecl_http and Cakephp 2.1... sigh, looks like I will have to wait till the next release. – Justin T. May 23 '12 at 08:35
-
Ugly work around would be to modify the CakePHP source and either rename/remove the class definition or wrap in in `if (!class_exists('HTTPResponse')) { // ... }` – DaveRandom May 23 '12 at 09:33
3 Answers
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.

- 310,957
- 84
- 592
- 636
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.
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.

- 155,785
- 88
- 678
- 743