Dependency Injection
In CakePHP
Goals
● Understanding what
dependency injection is, and a
few patterns to achieve it.
● You know how CakePHP does
dependency injection internally.
● You know what is coming next.
What is
Dependency
Injection?
… is a technique in which an object receives other
objects that it depends on. These other objects are
called dependencies. In the typical "using"
relationship the receiving object is called a client and
the passed (that is, "injected") object is called a
service.
-Wikipedia
OK...
Simple
Dependency
Injection
Constructors!
Constructors!
public function __construct(StripeClient $stripe)
{
$this->stripe = $stripe;
}
Seems simple
Real Projects are
Complicated
Factories
& Locators
& Registries
& Containers
Factories
Make one thing really well
Factories
$factory = new ShapeFactory($color);
$triangle = $factory->makeSided(3);
$square = $factory->makeSided(4);
Locators & Registries
Get or make a thing by a name
Locators & Registries
$registry = new LogRegistry($config);
$logger = $registry->get(‘default’);
Containers
Generalized creators of things
Containers
$container->add(ServiceConfig::class);
$container->add(HttpClient::class);
$container->add(StripeService::class)
->addArgument(ServiceConfig::class)
->addArgument(HttpClient::class);
$stripe = $container->get(StripeService::class);
Dependency
Injection in
CakePHP Today
Dependency Injection today
Using helpers in your templates:
echo $this->Html->link(‘Click me’);
Using caching:
Cache::write(‘key’, $value);
What about
application logic?
Until now CakePHP
didn’t help
Coming in 4.2
Injected Application Services
Application
Services?
Application Services
Contain one or more of:
● Logic that combines and coordinates multiple models.
● Logic that combines models with remote API.
● Logic that combines several remote APIs together.
● Logic that combines remote APIs and email.
● Logic that combines other services together.
● Or a variety of other things.
CakePHP Container
Coming in 4.2
Registering Services
public function register(
ContainerInterface $container
): ContainerInterface
{
$container->add(...);
$container->add(...);
return $container;
}
Bundling Services
class BillingServiceProvider extends AbstractServiceProvider
{
// A list of services provided
protected $provides = [
‘AppServiceInvoiceService’,
];
public function register()
{
$this->getContainer()->add(...);
}
}
// Add a service provider
$container->addServiceProvider(BillingServiceProvider::class);
Handling Configuration
use CakeCoreServiceConfig;
// Add a read-only wrapper around Configure.
$container->share(ServiceConfig::class);
// Add scalar values you can reference by name in
definitions.
$container->add(‘apiKey’, ‘abc123’);
Getting Services
If defined in the container, CakePHP will inject services into:
● Controller constructors
● Command constructors
Automatically based on reflection into:
● Controller actions
Controller Actions
// Parameters to controllers
public function upgrade(BillingService $billing) { … }
// Parameters to controllers with routing parameters
public function modify($id, BillingService $billing) { … }
Experimental Features
Backwards Compatibility
Experimental Features
Fewer backwards compatibility promises
Stabilization
After a release*
Containers
Will be the first experimental feature
Hopefully Stable in 4.3
Might be in 4.4 if we need to change
We need your help
Testing and finding use cases
Thank you

Dependency injection in CakePHP