# How to unload a class

- Thread: https://externals.io/message/14787
- Original archive: https://news-web.php.net/php.internals/14787
- Started by Carsten Harnisch on 2005-02-10T09:59:52+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #14787 — Carsten Harnisch — 2005-02-10T09:59:52+00:00

URL: https://externals.io/message/14787#14787

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

---

## #14788 — Johannes Schlueter — 2005-02-10T10:30:23+00:00

URL: https://externals.io/message/14787#14788 · In reply to: #14787

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

---

## #14797 — Sean Coates — 2005-02-10T15:28:43+00:00

URL: https://externals.io/message/14787#14797 · In reply to: #14788

Johannes Schlueter wrote:
> > […]
> 
> 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

---

## #14800 — Sebastian Bergmann — 2005-02-10T16:59:21+00:00

URL: https://externals.io/message/14787#14800 · In reply to: #14797

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

---

## #14801 — Sara Golemon — 2005-02-10T18:37:01+00:00

URL: https://externals.io/message/14787#14801 · In reply to: #14800

> > […]
>
> 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

---

## #14824 — Markus Fischer — 2005-02-11T07:25:19+00:00

URL: https://externals.io/message/14787#14824 · In reply to: #14801

Sara Golemon wrote:
> > […]
> 
> 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

---

## #14827 — George Schlossnagle — 2005-02-11T15:51:51+00:00

URL: https://externals.io/message/14787#14827 · In reply to: #14824

> Sara Golemon wrote:
> > […]
>
> 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
