SlideShare a Scribd company logo
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

More Related Content

PPTX
Inversion of Control and Dependency Injection
PDF
Pipeline oriented programming
PDF
これから Haskell を書くにあたって
PPTX
Présentation de ECMAScript 6
PPTX
How to Give a Domain for a Field in Odoo 17
PPTX
純粋関数型アルゴリズム入門
PPSX
Animations tris
PDF
PWNの超入門 大和セキュリティ神戸 2018-03-25
Inversion of Control and Dependency Injection
Pipeline oriented programming
これから Haskell を書くにあたって
Présentation de ECMAScript 6
How to Give a Domain for a Field in Odoo 17
純粋関数型アルゴリズム入門
Animations tris
PWNの超入門 大和セキュリティ神戸 2018-03-25

What's hot (20)

PDF
Functional Programming Patterns (NDC London 2014)
PDF
The lazy programmer's guide to writing thousands of tests
PDF
Performance Profiling in Rust
PDF
PHPの今とこれから2022
PDF
Chap1 Introduction à python
PDF
SymfonyLive Online 2023 - Is SOLID dead? .pdf
PDF
Domain Driven Design with the F# type System -- NDC London 2013
PDF
Effective Modern C++ 勉強会#7 Item 27
PDF
Domain Driven Design Made Functional with Python
PDF
Embedding Generic Monadic Transformer into Scala. [Tfp2022]
PDF
The Functional Programming Toolkit (NDC Oslo 2019)
PDF
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
PDF
すごい配列楽しく学ぼう
PDF
Hinemos Deep Dive~ジョブ編~
PPTX
Classes and objects
PPTX
Chapitre1: Langage Python
PDF
30 Symfony Best Practices
PPSX
Cours listes
PPT
COURS_PYTHON_22.ppt
PDF
PDOでデータベース接続と SQLインジェクション対策
Functional Programming Patterns (NDC London 2014)
The lazy programmer's guide to writing thousands of tests
Performance Profiling in Rust
PHPの今とこれから2022
Chap1 Introduction à python
SymfonyLive Online 2023 - Is SOLID dead? .pdf
Domain Driven Design with the F# type System -- NDC London 2013
Effective Modern C++ 勉強会#7 Item 27
Domain Driven Design Made Functional with Python
Embedding Generic Monadic Transformer into Scala. [Tfp2022]
The Functional Programming Toolkit (NDC Oslo 2019)
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
すごい配列楽しく学ぼう
Hinemos Deep Dive~ジョブ編~
Classes and objects
Chapitre1: Langage Python
30 Symfony Best Practices
Cours listes
COURS_PYTHON_22.ppt
PDOでデータベース接続と SQLインジェクション対策
Ad

Similar to Dependency injection in CakePHP (20)

PDF
Introduction to DI(C)
PDF
CakePHP
PDF
Symfony2 from the Trenches
PDF
Intro to CakePHP
PDF
Symfony2 - from the trenches
PDF
Dependency injection Drupal Camp Wrocław 2014
PDF
Dependency Injection in Drupal 8
KEY
Introducing CakeEntity
PDF
Nanos gigantium humeris insidentes (design patterns inside symfony 2)
PDF
Ch ch-changes cake php2
PDF
Cake php cookbook
ODP
Dependency Injection, Zend Framework and Symfony Container
DOCX
10 reasons to choose CakePHP as Framework
PDF
Why CakePHP Is Superior to Other Web Frameworks! Examine Its Pros & Cons For ...
ODP
Learning Symfony2 by practice
PDF
DIC To The Limit – deSymfonyDay, Barcelona 2014
PDF
CakePHP 3.0: Embracing the future
KEY
CakePHP 2.0 - PHP Matsuri 2011
PDF
Dependency Inversion and Dependency Injection in PHP
PDF
REST API with CakePHP
Introduction to DI(C)
CakePHP
Symfony2 from the Trenches
Intro to CakePHP
Symfony2 - from the trenches
Dependency injection Drupal Camp Wrocław 2014
Dependency Injection in Drupal 8
Introducing CakeEntity
Nanos gigantium humeris insidentes (design patterns inside symfony 2)
Ch ch-changes cake php2
Cake php cookbook
Dependency Injection, Zend Framework and Symfony Container
10 reasons to choose CakePHP as Framework
Why CakePHP Is Superior to Other Web Frameworks! Examine Its Pros & Cons For ...
Learning Symfony2 by practice
DIC To The Limit – deSymfonyDay, Barcelona 2014
CakePHP 3.0: Embracing the future
CakePHP 2.0 - PHP Matsuri 2011
Dependency Inversion and Dependency Injection in PHP
REST API with CakePHP
Ad

More from markstory (20)

PDF
Safer, More Helpful CakePHP
PDF
CakePHP - The Road Ahead
PDF
Future of HTTP in CakePHP
PDF
CakePHP mistakes made 2015
PDF
New in cakephp3
PDF
PHP WTF
PDF
CakePHP 3.0 and beyond
PDF
CakePHP mistakes made confoo 2015
PDF
CakePHP mistakes made
PDF
Performance and optimization CakeFest 2014
PDF
Road to CakePHP 3.0
PDF
Performance and optimization
PDF
OWASP Top 10 2013
PDF
CakePHP the yum & yuck
PDF
Introduction to Twig
PDF
Owasp top 10
PDF
Simple search with elastic search
PDF
Making the most of 2.2
PDF
Intro to continuous integration
PDF
Evented applications with RabbitMQ and CakePHP
Safer, More Helpful CakePHP
CakePHP - The Road Ahead
Future of HTTP in CakePHP
CakePHP mistakes made 2015
New in cakephp3
PHP WTF
CakePHP 3.0 and beyond
CakePHP mistakes made confoo 2015
CakePHP mistakes made
Performance and optimization CakeFest 2014
Road to CakePHP 3.0
Performance and optimization
OWASP Top 10 2013
CakePHP the yum & yuck
Introduction to Twig
Owasp top 10
Simple search with elastic search
Making the most of 2.2
Intro to continuous integration
Evented applications with RabbitMQ and CakePHP

Recently uploaded (20)

PDF
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
PPTX
GSA Content Generator Crack (2025 Latest)
PDF
Types of Token_ From Utility to Security.pdf
PPTX
Tech Workshop Escape Room Tech Workshop
PDF
Website Design Services for Small Businesses.pdf
PPTX
chapter 5 systemdesign2008.pptx for cimputer science students
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
Cost to Outsource Software Development in 2025
PDF
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
PPTX
assetexplorer- product-overview - presentation
PDF
iTop VPN Crack Latest Version Full Key 2025
PDF
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
PDF
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
Custom Software Development Services.pptx.pptx
PDF
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PPTX
Cybersecurity: Protecting the Digital World
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
GSA Content Generator Crack (2025 Latest)
Types of Token_ From Utility to Security.pdf
Tech Workshop Escape Room Tech Workshop
Website Design Services for Small Businesses.pdf
chapter 5 systemdesign2008.pptx for cimputer science students
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
Cost to Outsource Software Development in 2025
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
assetexplorer- product-overview - presentation
iTop VPN Crack Latest Version Full Key 2025
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
Why Generative AI is the Future of Content, Code & Creativity?
Designing Intelligence for the Shop Floor.pdf
Custom Software Development Services.pptx.pptx
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Cybersecurity: Protecting the Digital World
Weekly report ppt - harsh dattuprasad patel.pptx

Dependency injection in CakePHP