📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Zend Framework / Laminas Laminas Service Factories

Laminas Service Factories

6 min read
Use AbstractFactory and DelegatorFactory to create services dynamically or wrap existing ones.

Advanced Factories

// Abstract factory
class RepositoryAbstractFactory implements AbstractFactoryInterface {
    public function canCreate(ContainerInterface $c, string $name): bool {
        return str_ends_with($name, "Repository");
    }
    public function __invoke(ContainerInterface $c, string $name, ?array $opts = null): mixed {
        $adapter = $c->get(AdapterInterface::class);
        $table   = strtolower(str_replace("Repository"."", "", $name)) . "s";
        $gateway = new TableGateway($table, $adapter);
        return new $name($gateway);
    }
}