Skip to main content

Dependency Injection

Dependency injection is a programming technique that makes a class independent of its dependencies. It achieves that by decoupling the usage of an object from its creation.


Example
Setup/Dependency:
  • composer require symfony/dependency-injection
  • composer require symfony/yaml
  • composer require symfony/config
  • Create folder "src"
  • Add entry in composer.json "autoload":{"psr-4"{"DI\\": "src/"}
  • and then run composer dump-autoload -o

File: /Src/Mail.php
<?php
namespace DI;
class Mail{
    private $cc;
    function __construct($fromname,$fromemail)
    {
        $this->fromname = $fromname;
        $this->fromemail = $fromemail;
    }
    function sendMail($to,$subject,$message)
    {
         $headers = "MIME-Version: 1.0" . "\r\n";
         $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
         // More headers
         $headers .= 'From: '.$this->fromemail.'<'.$this->fromemail.'>' . "\r\n";
         mail($to,$subject,$message,$headers);
    }
}


File: /src/Notification.php

<?php
namespace DI;


class Notification{
    function __construct(Mail $mail)
    {
        $mail->sendMail("shanky@tech.com","Test Class","Welcome to Dependency Injection course.");
    }

 }


File: /index.php
<?php
require_once('vendor/autoload.php');
use DI\Mail;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

$container->setParameter('mailer.from_name', 'sendmail');
$container->setParameter('mailer.from_email', 'sendmail');
$container->register('mail', 'DI\Mail')->addArgument('%mailer.from_name%','%mailer.from_email%');;
$container->register('notification', 'DI\Notification')->addArgument(new Reference('mail'));


Comments

Popular posts from this blog

Splitting Comma-Separated Values In MySQL

CREATE TABLE numbers (n INT);  INSERT INTO numbers VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9),     select id, substring_index( substring_index(email_recipients, ',', n), ',', -1 ) as email from dashboards join numbers on char_length(email_recipients) - char_length(replace(email_recipients, ',', '')) >= n - 1     Reference Site : https://www.sisense.com/blog/splitting-comma-separated-values-in-mysql/

Reading Large JSON file through PHP Script

 <?php /* cat mexico.geojson | wc sed ':a;N;$!ba;s/\n/ /g' mexico.geojson  > mexico.geojson_out1 cat mexico.geojson_out1 | wc http://localhost/Utility/cord/largefile.php?filename=mexico.geojson_out1 sed -E 's/\{ "type": "FeatureCollection", "name": "mexico_administrative_osm_province_boundaries", "crs": \{ "type": "name", "properties": \{ "name": "urn:ogc:def:crs:OGC:1.3:CRS84" \} \}, "features": \[//g' ./mexico.geojson_out1  > mexico.geojson_out2 http://localhost/Utility/cord/largefile.php?filename=mexico.geojson_out2 sed -E 's/(\[|\{|\}|\])/ \1 /g' ./mexico.geojson_out2 > ./output/mexico.geojson_output http://localhost/Utility/cord/largefile.php?filename=output/mexico.geojson_output */ ini_set('max_execution_time', '500'); //300 seconds = 5 minutes ini_set('memory_limit', '1G');   //ini_set('max_execution...