GithubHelp home page GithubHelp logo

rioastamal-examples / introduction-to-dependency-injection-in-php Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 20 KB

Introduction to dependency injection in PHP with examples

License: MIT License

PHP 100.00%
php dependency-injection example email

introduction-to-dependency-injection-in-php's Introduction

About

This example contains simple introduction to Dependency Injection (DI) in PHP. In simple term Dependency Injection is design principle where dependencies of an object are provided (injected) as an argument rather than the object creating it internally inside its own class.

This example uses Mailer class to demonstrate the use of dependency injection. Here's how we build Mailer object using DI principle.

$email = new Email($to, $subject, $body, $from);
$transport = new PhpMailTransport();
$mailer = new Mailer($email, $transport);
$mailer->send();

As you can see above we are injecting the dependencies of Mailer class as an arguments of the class constructor.

How To Run

To run this example you need to clone or download the source code from github. You will also need composer to install all the dependency packages.

$ git clone [email protected]:rioastamal/introduction-to-dependency-injection-in-php.git
$ cd introduction-to-dependency-injection-in-php
$ composer install
$ php -S 127.0.0.1:8080 -t public/

Open your web browser point to http://localhost:8080 to test the application. Keep in mind, if you use PhpMailTransport class you need to provide working local smtp server.

To run the unit test simply issue command below.

$ phpunit
PHPUnit 6.5.7 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 159 ms, Memory: 8.00MB

OK (1 test, 5 assertions)

Improvement

You can create SmtpMailTransport class to support sending email using SMTP protocol and also SMTP authentication.

Read More

My complete article about introduction to dependency injection in PHP can be found on link below. It's in Bahasa Indonesia.

https://www.linkedin.com/pulse/mengenal-dependency-injection-di-pada-php-rio-astamal/

Author

This example is written by Rio Astamal [email protected]

License

This example and it sources is open source licensed under MIT license.

introduction-to-dependency-injection-in-php's People

Contributors

rioastamal avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.