nette/di
Nette DI is a fast, configurable dependency injection container for PHP. Compile-time container generation boosts performance, while extensions, autowiring, and service definitions make complex apps easy to wire. Integrates smoothly with the Nette framework or standalone.
Start by installing via Composer: composer require nette/di. Define services in a NEON config file (e.g., services.neon) using simple key-value syntax—Nette auto-detects services by type-hints and uses autowiring by default. For a basic setup:
services:
- App\Model\UserRepository
App\Service\EmailService: [@App\Model\UserRepository]
parameters:
debugMode: %env.ADAPTIVE_DEBUG%
Then compile the container:
$compiler = new Nette\DI\Compiler();
$compiler->addConfig(['services' => [...]]); // or load from NEON
$container = $compiler->compile();
First use case: Inject services via constructor autowiring in your classes—no configuration needed for most cases. Leverage richer PHPDoc and native types for better IDE support (e.g., @param App\Model\UserRepository $userRepo).
@param array<App\Model\User> $users) improve autowiring for collections.services:
cacheFactory:
factory: App\Factory\CacheFactory::create()
setup:
- setTtl(3600)
%env.VAR% or parameters section—expanded at compile time. Tip: Native types in ContainerBuilder ensure stricter validation for runtime parameters.lazy: true to defer instantiation:
services:
dbConnection:
lazy: true
factory: PDO
Nette\DI\CompilerExtension—now with PHPStan-clean internals, reducing edge-case bugs.#[Nette\DI\Attributes\Inject] for injection where constructors are impractical (e.g., controllers). New: IDEs now better infer types from PHPDoc in attributes.nette/di rules.%parameters% expands only static params—avoid for runtime values. Use getParameter() at runtime instead.?Type) or union types improve reliability. New: Native types in Resolver now enforce stricter checks.@self in setup or lazy loading to break cycles.$tempDir—reduces cold-start overhead. New: Tracy’s ContainerPanel now uses n:attributes for cleaner debugging.$container->getService('debug Tracy') or dump()—new Latte rewrite makes the panel more intuitive.@param array<ClassName> in PHPDoc for collections (e.g., array<App\Model\User>).lazy and group extensions in separate config files to reduce compile time.Compiler, Container, and Resolver improves autocompletion and type hints. Example:
/** @param array<App\Service\LoggerInterface> $loggers */
public function __construct(array $loggers) {}
How can I help you explore Laravel packages today?