I have a service layer in my Zend Framework 2 application. In this layer, I have an abstract base class which all service classes extend, like demonstrated below.
Base class:
namespace User\Service;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;
class AbstractService implements ServiceManagerAwareInterface {
protected $serviceManager;
public function setServiceManager(ServiceManager $serviceManager) {
$this->serviceManager = $serviceManager;
}
public function getServiceManager() {
return $this->serviceManager;
}
}
A service class:
namespace User\Service;
use User\Service\AbstractService;
class UserService extends AbstractService {
public function register($id, $username, $password) {
/* ... */
}
}
The thing is that the base class will be used throughout my modules (each module will have a service layer) and I am wondering where the best place to store it is. I have exactly the same scenario for my database layer. Off the top of my head, I can think of a few possibilities:
- A copy in every module. Not an elegant solution and not easy to maintain.
- One copy in an “Application” module and each module uses this one copy. Creates cross-module dependencies.
- In the
vendordirectory and thus outside of any module directory. I am not too familiar with Composer yet, but I guess I could add it as a dependency there?
So basically the question is where to place classes that are used by several modules. It is not difficult to get it to work, but the problem is doing it the best possible way.
I think a good practice is unit all of your base classes and functions into a independent common module, your other modules will depend on this common module.
There are a lot references you could take a look:
After you have a common module, you could define the module depending relationships by composer, then just one line command to install all depend modules.