How to unload a class

php.internals

Carsten Harnisch

21 years ago
I would like to "unload" a class loaded previously by require_once. Mainly I am working on a daemon where handler-classes should be loaded and unloaded on demand. I did not find a way to drop a class from memory and reload it again from a (maybe changed) file. There are some work-a-rounds on the net like using eval or create_function. But is there any clean way to drop a class ? Carsten

Johannes Schlueter

21 years ago
Hi, Carsten Harnisch wrote:
> I would like to "unload" a class loaded previously by require_once. > Mainly I am working on a daemon where handler-classes should be loaded and > unloaded on demand.
You could remove a class using classkit ... http://pecl.php.net/package/classkit johannes

Sean Coates

21 years ago
Johannes Schlueter wrote:
>>I would like to "unload" a class loaded previously by require_once. >>Mainly I am working on a daemon where handler-classes should be loaded and >>unloaded on demand. > > You could remove a class using classkit ...
No, you could remove the methods that belong to the class and/or rename the class, but class removal isn't possible. (What would happen to the instanciated objects?) S

Sebastian Bergmann

21 years ago
Sean Coates wrote:
> What would happen to the instanciated objects?
Only allowing class unloading for classes that have no instantiated objects would be an option.
-- Sebastian Bergmann http://www.sebastian-bergmann.de/ GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69

Sara Golemon

21 years ago
>> What would happen to the instanciated objects? > > Only allowing class unloading for classes that have no instantiated > objects would be an option. >
And how would you know if it'd been instantiated? Even if you recursively looped through every variable hash from EG(symbol_table) on down, there'd still be the possibility that one or two objects would be hiding in a resource somewhere (i.e. php_stream_context). This is also why delete() doesn't (and likely won't ever) exist in PHP. Anyway, sounds like what he wants is classkit_import(). -Sara

Markus Fischer

21 years ago
Sara Golemon wrote:
>>>What would happen to the instanciated objects? >> >>Only allowing class unloading for classes that have no instantiated >>objects would be an option. >> > > And how would you know if it'd been instantiated? Even if you recursively > looped through every variable hash from EG(symbol_table) on down, there'd > still be the possibility that one or two objects would be hiding in a > resource somewhere (i.e. php_stream_context).
Is there a technical limitation to loop through every internal hash which could possible hold an instantiated object? If there would exist functions like - class_has_instanciated_objects() - class_get_instanciated_objects() I guess noone would expect them to be quick or fast. Of course there's the fact to respect the class hierarchy which adds complexity. I can imagine that optimizers and such would have problems with it. But albeit, technically? - Markus

George Schlossnagle

21 years ago
On Feb 11, 2005, at 2:25 AM, Markus Fischer wrote:
> Sara Golemon wrote: >>>> What would happen to the instanciated objects? >>> >>> Only allowing class unloading for classes that have no instantiated >>> objects would be an option. >>> >> And how would you know if it'd been instantiated? Even if you >> recursively looped through every variable hash from EG(symbol_table) >> on down, there'd still be the possibility that one or two objects >> would be hiding in a resource somewhere (i.e. php_stream_context). > > Is there a technical limitation to loop through every internal hash > which could possible hold an instantiated object?
Yes. There are obscure, arbitrary places these things can live. George

Carsten Harnisch

21 years ago
Finally I got a chance to test classkit. Anyway installation with "pear install classkit" went through with no errors and after activation in php.ini the module shows up as installed. Anyway the function does not work (class_import) I am still getting the same error as re-require_once a class-file : "Fatal error: Cannot redeclare class XXX in file" We are runnning php4 on debian on a sun e3000. Could this be the glitch that the pecl-classkit does not run on php4 or not on the sparc machine ? Carsten -----Ursprüngliche Nachricht----- Von: Johannes Schlueter [mailto:johannes@php.net] Gesendet: Donnerstag, 10. Februar 2005 11:30 An: internals@lists.php.net; Carsten Harnisch Betreff: [PHP-DEV] Re: How to unload a class Hi, Carsten Harnisch wrote:
> I would like to "unload" a class loaded previously by require_once. > Mainly I am working on a daemon where handler-classes should be loaded > and unloaded on demand.
You could remove a class using classkit ... http://pecl.php.net/package/classkit johannes
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php

Sara Golemon

21 years ago
> Finally I got a chance to test classkit. Anyway installation with "pear > install classkit" went through with no errors and after activation in > php.ini the module shows up as installed. Anyway the function does not > work > (class_import) I am still getting the same error as re-require_once a > class-file : > > "Fatal error: Cannot redeclare class XXX in file" >
(1) The class is a user defined ( not internal ) class yes? (2) Can you provide some reproducing code? (3) This probably isn't a php.internals topic anymore so feel free to reply direct (pollita@php.net)
> We are runnning php4 on debian on a sun e3000. > Could this be the glitch that the pecl-classkit does > not run on php4 or not on the sparc machine ? >
Doubtful, its functionality is just a reordering of various parts of the ZE execution cycle. -Sara