Dependency Injection in XOOPS¶
Version Compatibility
| Feature | XOOPS 2.5.x | XOOPS 2026 |
|---|---|---|
| Manual DI (constructor injection) | ✅ Available | ✅ Available |
| PSR-11 Container | ❌ Not built-in | ✅ Native support |
\Xmf\Module\Helper::getContainer() | ❌ 2026 only | ✅ Available |
In XOOPS 2.5.x, use manual constructor injection (passing dependencies explicitly). The PSR-11 container examples below are for XOOPS 2026.
Overview¶
Dependency Injection (DI) is a design pattern that allows components to receive their dependencies from external sources rather than creating them internally. XOOPS 2026 introduces PSR-11 compatible DI container support.
Why Dependency Injection?¶
Without DI (Tight Coupling)¶
class ArticleService
{
private ArticleRepository $repository;
private EventDispatcher $dispatcher;
public function __construct()
{
// Hard dependencies - difficult to test and modify
$this->repository = new ArticleRepository(new XoopsDatabase());
$this->dispatcher = new EventDispatcher();
}
}
With DI (Loose Coupling)¶
class ArticleService
{
public function __construct(
private readonly ArticleRepositoryInterface $repository,
private readonly EventDispatcherInterface $dispatcher
) {}
}
PSR-11 Container¶
Basic Usage¶
use Psr\Container\ContainerInterface;
// Get the container
$container = \Xmf\Module\Helper::getHelper('mymodule')->getContainer();
// Retrieve a service
$articleService = $container->get(ArticleService::class);
// Check if service exists
if ($container->has(ArticleService::class)) {
// Use the service
}
Container Configuration¶
// config/services.php
use Psr\Container\ContainerInterface;
return [
// Simple class instantiation
ArticleRepository::class => ArticleRepository::class,
// Interface to implementation binding
ArticleRepositoryInterface::class => ArticleRepository::class,
// Factory function
ArticleService::class => function (ContainerInterface $c): ArticleService {
return new ArticleService(
$c->get(ArticleRepositoryInterface::class),
$c->get(EventDispatcherInterface::class)
);
},
// Shared instance (singleton)
'database' => function (): XoopsDatabase {
return XoopsDatabaseFactory::getDatabaseConnection();
},
];
Service Registration¶
Auto-wiring¶
// The container automatically resolves dependencies
// when type hints are available
class ArticleController
{
public function __construct(
private readonly ArticleService $service,
private readonly ViewRenderer $renderer
) {}
}
// Container creates ArticleController with its dependencies
$controller = $container->get(ArticleController::class);
Manual Registration¶
// config/services.php
return [
ArticleService::class => [
'class' => ArticleService::class,
'arguments' => [
ArticleRepositoryInterface::class,
EventDispatcherInterface::class,
],
'shared' => true, // Singleton
],
'article.handler' => [
'factory' => [ArticleHandlerFactory::class, 'create'],
'arguments' => ['@database'], // Reference other service
],
];
Constructor Injection¶
Preferred Approach¶
final class ArticleService
{
public function __construct(
private readonly ArticleRepositoryInterface $repository,
private readonly EventDispatcherInterface $dispatcher,
private readonly LoggerInterface $logger
) {}
public function create(CreateArticleDTO $dto): Article
{
$this->logger->info('Creating article', ['title' => $dto->title]);
$article = Article::create($dto);
$this->repository->save($article);
$this->dispatcher->dispatch(new ArticleCreatedEvent($article));
return $article;
}
}
Method Injection¶
For Optional Dependencies¶
class ArticleController
{
public function __construct(
private readonly ArticleService $service
) {}
public function show(int $id, ?CacheInterface $cache = null): Response
{
$cacheKey = "article_{$id}";
if ($cache && $cached = $cache->get($cacheKey)) {
return $this->render($cached);
}
$article = $this->service->findById($id);
$cache?->set($cacheKey, $article, 3600);
return $this->render($article);
}
}
Interface Binding¶
Define Interfaces¶
interface ArticleRepositoryInterface
{
public function findById(int $id): ?Article;
public function save(Article $article): void;
public function delete(Article $article): void;
}
Bind Implementation¶
// config/services.php
return [
ArticleRepositoryInterface::class => XoopsArticleRepository::class,
// Or with factory
ArticleRepositoryInterface::class => function (ContainerInterface $c) {
return new XoopsArticleRepository(
$c->get('database')
);
},
];
Testing with DI¶
Easy Mocking¶
class ArticleServiceTest extends TestCase
{
public function testCreateArticle(): void
{
// Create mocks
$repository = $this->createMock(ArticleRepositoryInterface::class);
$dispatcher = $this->createMock(EventDispatcherInterface::class);
$logger = $this->createMock(LoggerInterface::class);
// Inject mocks
$service = new ArticleService($repository, $dispatcher, $logger);
// Set expectations
$repository->expects($this->once())->method('save');
$dispatcher->expects($this->once())->method('dispatch');
// Test
$dto = new CreateArticleDTO('Title', 'Content');
$article = $service->create($dto);
$this->assertInstanceOf(Article::class, $article);
}
}
XOOPS Legacy Integration¶
Bridging Old and New¶
// Get service from container in legacy code
function mymodule_get_articles(int $limit): array
{
$container = \Xmf\Module\Helper::getHelper('mymodule')->getContainer();
$service = $container->get(ArticleService::class);
return $service->findRecent($limit);
}
Wrapping Legacy Handlers¶
// config/services.php
return [
'article.handler' => function () {
return xoops_getModuleHandler('article', 'mymodule');
},
ArticleRepositoryInterface::class => function (ContainerInterface $c) {
return new LegacyArticleRepository(
$c->get('article.handler')
);
},
];
Best Practices¶
- Inject Interfaces - Depend on abstractions, not implementations
- Constructor Injection - Prefer constructor over setter injection
- Single Responsibility - Each class should have few dependencies
- Avoid Container Awareness - Services shouldn't know about the container
- Configure, Don't Code - Use configuration files for wiring
Related Documentation¶
- PSR-11-Dependency-Injection-Guide - PSR-11 implementation
- Service-Layer - Service pattern
- Testing - Testing with DI
- XOOPS-2026-Architecture - Architecture overview