Loading two classes with the same name

2

In a legacy system, I have situation that 2 classes have the same name. A legacy and a recent version (using MVC framework). There are cases that in the new version of the class I need to call the same (old) or another class from the old system. Unfortunately there is no way to eliminate the old class (to maintain compatibility) and it would be very difficult to migrate to the new version, having to keep the new and old. The old system was not designed to call new methods, only the new one can call the old.

After discussion, we ended up choosing to create a class that serves as a bridge for the old classes of the system. We wanted to create a kind of sandbox, where we could call old classes (with the same name as the new ones) and return their results in isolation.

Something that would solve (and would be the correct one) would be to use namespaces , but bar on the problem of the rework of adapting both the old and the new part. So we discarded that.

We end up injecting a custom aautoload () when we invoke this class of "bridge". Where to check if the called name exists in the old part of the system, if it exists, takes its contents, gives append in its name, modifies the name and does not cause conflict, checks the class code and makes substitutions for other class names to ensure that everything dependent is invoked. The content is saved in a temporary file and included, after the delete is deleted the temporary file.

I think seeing the class will understand better: link

I want to know if there is a more efficient way to do this, without appealing to a system hack. We have seen about the php Runkit_Sandbox ( link ) and uopz ( link ), but I can not imagine how it would be used (and if it really works). We have another problem that the version of our PHP is 5.3 (unfortunately it is laborious to update version, because of the legacy system).

    
asked by anonymous 20.01.2015 / 17:43

1 answer

1

First, do this in deploy instead of doing a loader. It is easier and more efficient for your deploy script to do this class renaming once and only choose the PHP file.

Runkit in production is a bad idea.

Any other solution (except refactoring) will still be worse - and I would totally recommend a refactoring. Note, if you just need to change class names you can do sed or replace in your own IDE with a regex, just like you already put in your autoloader, and solve that forever. You will only have problems if the code abuses evals, and you will detect any problems on the fly when running your test suite.

    
20.01.2015 / 22:43