Reputation: 3485
Should I wrap them in a service class or inject directly?
1) Use directly. Cons: no contract, strong dependency on things I cannot control. Pros: simple.
namespace App;
use AlienPackageClass;
class MyClass {
protected $alien_class;
public function __construct(AlienPackageClass $class){
$this->alien_class = $class;
}
}
2) Use with a wrapper. Cons: more writing. Pros: strong contract.
namespace App\Services;
use AlienPackageClass;
class AlienPackageClassService implements AlienPackageClassInterface {
protected $alien_class;
public function __construct(AlienPackageClass $class){
$this->alien_class = $class;
}
public function getSomething(){
}
}
interface AlienPackageClassInterface {
public function getSomething(): bool;
}
In this case, I could inject AlienPackageClassInterface
instead of AlienPackageClass
.
Which way to go?
Upvotes: 0
Views: 108
Reputation: 2271
'strong dependency on things I cannot control'
is a keyword for me. Don't want to have things I cannot control in my code.
And specially, if you think you might want to implement another instance of AlienPackageClassInterface
down the road, you might want to go ahead with the second option.
But then again, as they say, these things need not be religious. Which ever scenario best solve the business goal should be the one to win.
*Also following this thread for others responses.
Upvotes: 1