Search code examples
phpnamespacesresetclassnameautoloader

PHP - reset autoload and declared classes


In PHP I have a function that sets auto-loaders based on which child-site is selected within the parent-site.

The resetting of the auto-loaders goes excellent with the spl_autoload_unregister function.

However, what I notice is that when I load a class-file that was already initiated before the child-site, that this 'old' class-file is still used after resetting the auto-loaders.

Example:

Parent-site loads example\namespace\RouterClass from c:\parent\site\RouterClass

Then I reset all auto-loaders and initiate the child-site and set the correct auto-loaders for the child-site.

Then the child-site loads example\namespace\RouterClass but instead of getting this file via the new auto-loader from c:\child\site\RouterClass it gets the class from the old file as defined by the parent-site.

I see the class registered via the function:

get_declared_classes()

But I'm not able to reset this variable list. If a reset of this class-cache is possible then PHP has to start a new search for this RouterClass and will check the child-site auto-loader to find it.

Second option I checked was runkit_method_redefine, but there is no similar function for classes like runkit_class_redefine.

Does anyone have an idea of how to reset the declared class list? Or how to achieve the above without renaming all namespaces of the child-site.


Solution

  • Not possible. Once a class is declared, it's there for good.

    If you need to define autoloaders dynamically, you need to do so before any of the affected classes are loaded.