Answer the question
In order to leave comments, you need to log in
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
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.
Maybe with the help of runkit this can be done ... It seems that it is impossible with standard tools.
Stop suffering bullshit.
If you don't need smarti, don't load it into memory in the first place.
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.
было
class MyModule extends BasicModule
стало
class MyModule extends BasicModuleNoSmarty
было
class MyModule extends BasicModule
class BasicModule extends AbstractModule ( а может это и базовый класс, тогда чуть проще)
стало
class MyModule extends BasicModuleNoSmarty
class BasicModule extends BasicModuleNoSmarty{
тут добавление Smarty куда надо
}
class BasicModuleNoSmarty extends AbstractModule {
тут все, что было в обычном модуле но без шаблонизатора.
}
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question