tags:

views:

288

answers:

4

I realize that you cannot "undeclare" or "redeclare" a class in PHP, nor can I use extension methods as in C# > 3.0. So, here's my situation.

I'm working with someone else's code base. They must have been using a slightly older version of PHP where a DateTime class did not exist, so they included an open source library that adds Time, Date, and DateTime classes. Well, I go to deploy this to the client's server, and this class is already defined. To resolve the crash it causes, I add a "if class is not defined" check, however it seems that (as you'd expect) the api of the classes are not exactly the same, and somewhere in the code it may or may not be using undefined methods of the DateTime class included with the version of the language being used.

Are there any ways to unload a class, or any creative ways you can think of to get around this. I'd prefer not to have to search all files for all references and maybe rename the class. That sounds messy, but I guess it may be my only option. I'd much prefer a fix that happens in one place and one place only.

Thanks.

+3  A: 

You may want to enclose the class definition inside a Namespace. Then you declare a "using namespace xxx" ( I don't remember the exact PHP syntax ) inside every file that uses the current DateTime class.

This is not exactly a "one place and one place only" solution, but, at least you don't have to replace every reference to the class, just reference the namespace at the beginning of the files.

Hope this helps.

Petruza
This only works in PHP >= 5.3, which isn't that widely used yet
Tom Haigh
Still it's in option one can look into, especially if it's a show stopper otherwise...
VolkerK
Good to know ..
Petruza
+1  A: 

Perhaps this can help: Unload dynamic class in PHP.

Mr. Smith
+1  A: 

I would attempt to migrate the code base to the more recent version of PHP. If you are going to use the code in production on the new PHP version, then you need to test it on that version anyway. If you do not want to invest the efforts to update and test the code base you should probably require the older version to be installed on the target system. Otherwise there is no way you can guarantee that the code you have works correctly.

And with proper tools searching through a ton of files and identifying all occurrences of those classes isn't too hard.

Robert Klemme
+2  A: 

Some good ideas here...

Using a Namespace - suggested by Petruza - is probably the best way to handle it... but it's PHP 5.3 only.

Alternatively, Robert's suggestion of refactoring the code is a reasonable idea but will likely take a good deal of time and effort. Since this will have to be done eventually, you might want to do that.

Alternatively, if getting the system online is the top priority and you can refactor it afterwards, you might be able to rename the class and all references to it. By adding a prefix - DateTime to company_DateTime - you're namespacing it the old way and your classes should coexist without much difficulty. Of course, if you have to update the supporting libraries any time soon, this is going to make life more difficult, so don't blindly jump into this one...

CaseySoftware