GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — DesignPatternsPHP/Structural/DependencyInjection at main · DesignPatternsPHP/DesignPatternsPHP

Expand file tree

/

DependencyInjection

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

DependencyInjection

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Tests
Tests
 
 
uml
uml
 
 
DatabaseConfiguration.php
DatabaseConfiguration.php
 
 
DatabaseConnection.php
DatabaseConnection.php
 
 
README.rst
README.rst
 
 

README.rst

Outline

Dependency Injection

Purpose

To implement a loosely coupled architecture in order to get better testable, maintainable and extendable code.

Usage

DatabaseConfiguration gets injected and DatabaseConnection will get all that it needs from $config. Without DI, the configuration would be created directly in DatabaseConnection, which is not very good for testing and extending it.

Examples

  • The Doctrine2 ORM uses dependency injection e.g. for configuration that is injected into a Connection object. For testing purposes, one can easily create a mock object of the configuration and inject that into the Connection object
  • many frameworks already have containers for DI that create objects via a configuration array and inject them where needed (i.e. in Controllers)

UML Diagram

Alt DependencyInjection UML Diagram

Code

You can also find this code on GitHub

DatabaseConfiguration.php

.. literalinclude:: DatabaseConfiguration.php
   :language: php
   :linenos:

DatabaseConnection.php

.. literalinclude:: DatabaseConnection.php
   :language: php
   :linenos:

Test

Tests/DependencyInjectionTest.php

.. literalinclude:: Tests/DependencyInjectionTest.php
   :language: php
   :linenos:

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.