Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling classes with many versions, adapter or factory?

Tags:

php

I am working with a third party library that has new versions of the same(mostly) classes every 3-4 months. I don't want to have to update every class that uses them every time a new version is out.

The library is structured in the following manner where the service uses the class of the same version. v201609/myClass v201609/myService v201701/myClass v201701/myService

I can't feed an adapter into the service, so I will have to construct the actual class. To illustrate: $myService->handle(new MyClass);

My solution where factories use and build the actual classes: (MyServiceFactory::build())->handle(MyClassFactory::fromModel($myModel));

So every time a new version of the classes are published, I will have to update all my factories.

Questions:

  1. Is this the optimal approach to this problem?
  2. Is factory the correct naming convention to this approach?
like image 835
JC Lee Avatar asked Jan 07 '17 12:01

JC Lee


1 Answers

The problem is that they use version number in any position of namespace. In my opinion, your solution is not that bad.

The problem is when you want you type declaration in code, but this could generate only drawback. Then the same go to Factories. Then every few month you need to update them.

Autoloader itself, won't resolve this, as you still declared class in factory. Additionally, you want omit problem coupling project to version - in result, you will omit everywhere type declaration for this objects.

Good advice is to use Integration testing (PHPUnit), this will find any problem with Data structure after update, you will need this.

Second advice - in constructor, send actual version to load and use loading by string name

return new $fullClassName()

with correct class name and version. Then you will change this only in you config file, for given factory. Don't use use for following classes then.

I hope, this will fully resolve you problem.

like image 191
timiTao Avatar answered Sep 29 '22 17:09

timiTao