php multi version of class
I want develop a php module system like pypi or gems. The biggest problem is the class's multi version.
In case there is a cache module, and is depended by other module, one day cache module updated, for example, previous version is 0.1, and now updated to 0.2. The modules that depend on this cache module may not work as well.
One way to override this problem is limit a module can not change api. Only can add new method or change method's internal implement. but it still may cause problems, eg: 1.0 works ok, when update to 2.0 , cause not tested so well, bring some new bugs.
Multi class version is a more preferred way to deal this. But it seems hard using php without namespace. or
class Cache1 {}
class Cache2 {}
//...
It is not so convient both for开发者_如何学编程 module developer and users.
So any suggestions?
Edit
in case database and auth module both rely on cache module, but db rely on cache 1.0, auth rely on cache 2.0, cause cache 2.0 add some new methods, if they are two modules and both called 'class Cache', there will be error like "can't redeclare class Cache". if just update cache to 2.0, the database module may broke.(database module may only want to depend cache 1.0, because it just works)
I see two solutions to your problem:
- Upgrade to PHP 5.3 and use namespaces (might not be an option).
- Write a lot of Unit Tests (you should be doing this anyway).
Frankly, the unit tests should CYA pretty effectively when working on new versions of your software. If anything is un-fixable, or a new change just breaks a test completely, you'll at least be fully aware of it, and be able to offer a migration guide to your users.
You might want to keep meta data of the modules including version numbers of dependencies. I think this is the best solution because there are tons of other reasons why you want to keep track of version numbers. Also if you do this you you can automaticaly download the correct (might be older version) of dependencies.
About the case i think the initial question is about: You mean that if a user has multiple versions of a module installed and a different module needs module of the older version how to deal with this? I think you might want to provide code to include the correct versions of the library and don't include (include_path?) the others. You might want to take an example of http://gembundler.com/ which is open source. This is really a great dependency manager for rails.
It seems like the core solution is you need to implement some standard unit tests to avoid the problems. And you should be using a version control system, such that you can revert back to older versions of your code files.
So I'm not sure exactly what you're trying to accomplish, but you could do something like:
class Cache extends Cache1 { /* empty */ }
Code would just use the Cache
class, which can easily be switched to extend the exact version you want.
精彩评论