Re: Unload/reload included class
| From: | David Harkness | Date: | Wed, 05 Jan 2011 16:49:24 +0000 |
| Subject: | Re: Unload/reload included class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-310506@lists.php.net to get a copy of this message | ||
On Tue, Jan 4, 2011 at 4:44 PM, Patrik Pomichal
<patrik.pomichal@gmail.com>wrote:
> Loading plugin is easy, when my app found new file in plugins dir,
> include and register it. But if i change the plugin source i must restart
> the app to reload it.
If you don't mind the resource burn (memory) of loading a lot of code over
time without flushing it out when not needed, you could have the plugin
files contain the methods without the enclosing class declaration. When you
(re)load a plugin, you create a dynamic class name using a counter or some
other uniqueness mechanism, build a string containing a class declaration
wrapped around the file's contents, and use eval() to load it.
You could of course let the coder put a class name into the plugin file if
that looks nicer and do a string replace on it with your dynamic name. The
key is that you cannot unload classes in PHP AFAIK so you must come up with
a new class name each time. The old compiled bytecode will remain, but you
can destroy the old plugin instance to regain a little memory.
David