M
M
Maxim Barulin2013-04-26 07:36:37
PHP
Maxim Barulin, 2013-04-26 07:36:37

Unload a class from memory

Hi Habr!
There was such a question. Is it possible to unload a previously loaded class? Those. before it is connected in one of the files, but I need to unload it somehow. I googled mana, didn't find anything similar. I draw your attention, not a class object, but the class itself, to be precise, it is not alone there, it seems to me that there are several dozen of them, but this does not change the essence.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
M
mayorovp, 2013-04-26
@mayorovp

Better fix the architecture by introducing a new base class without Smarty initialization, or by using lazy object creation. Creating a complex object in order to dispose of it as unused is very bad style.

Y
youROCK, 2013-04-26
@youROCK

Maybe with the help of runkit this can be done ... It seems that it is impossible with standard tools.

E
egorinsk, 2013-04-26
@egorinsk

Stop suffering bullshit.
If you don't need smarti, don't load it into memory in the first place.

V
Vampiro, 2013-04-26
@Vampiro

There is a system, it uses a template engine (Smarty), which is initialized as a field of the parent class of all modules. There is a module that does not need a template engine.

Find the parent module. copy it, name it something like BaseModuleNoSmarty. Throw out everything that is connected with smarty from there and inherit from it. That is, you have two options, fast and good:
good:
было
class MyModule extends BasicModule
стало
class MyModule extends BasicModuleNoSmarty

and the second, with a lot of refactoring:
было
class MyModule extends BasicModule
class BasicModule extends AbstractModule ( а может это и базовый класс, тогда чуть проще)
стало
class MyModule extends BasicModuleNoSmarty
class BasicModule extends BasicModuleNoSmarty{
тут добавление Smarty куда надо
}
class BasicModuleNoSmarty extends AbstractModule {
тут все, что было в обычном модуле но без шаблонизатора.
}

This is if you don't want to redo the base class implementation of all modules.

C
cyberorg, 2013-04-26
@kyberorg

As far as I understand the process, PHP's analogue of the "preprocessor" goes through and loads all include/require into memory. This is if these simple directives are used to load classes. In this case, it will not work to unload something from memory (or it will work, but obviously not by means of PHP itself). Since PHP does not have a classpath that can be changed at runtime.
However, PHP has such a thing as spl_autoload. And if a class is loaded using such a loader, then it is possible to unload it: php.net/manual/en/function.spl-autoload-unregister.php
>it is not alone there, it seems to me there are several dozen of them
. This can be found through the reflection mechanisms.

M
Max, 2013-04-26
@7workers

An interesting task. And try pulling out the get_declared_classes() help class names and overriding them with empty: class Foo {} using eval() for example. Tell me what the effect was.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question