Uncategorized

Dependency Injection

If you are starting to learn about Drupal 8, you must have come across a term called “Dependency Injection”. If you are wondering what it means, then this is the post you should read. Through examples, you will learn why dependency injection is useful for decoupling the code as well as unit testing effectively.

In the post on PHP Inheritance, we created HondaAccord and ChevyRam classes, both of which extend the Vehicle class. Vehicle has getWarrantyLeft(), drive() and getMilesDriven() methods. getWarrantyLeft() method is being overridden by ChevyRam while HondaAccord has a trunk and implements putInTrunk() and takeFromTrunk() methods. Vehicle also has colorwarranty and milesDriven properties.

Let’s also assume that there is a Person class and each person owns a vehicle. We are initializing this vehicle in the constructor of the Person class. Person has methods travel() and getDistanceTraveled()travel() method uses Vehicle‘s drive() method and getDistanceTraveled() method uses the Vehicle‘s getMilesDriven() method. Here is how Vehicle.php and Person.php files look:

Vehicle.php

<?php
 
/**
 * Vehicle class.
 */
class Vehicle {
 
  // Color of the vehicle.
  public $color;
 
  // Miles driven.
  private $milesDriven;
 
  // Warranty available.
  public $warranty;
 
  /**
   * Default constructor.
   *
   * @param string $color
   *  Color of the vehicle.
   * @param int $milesDriven
   *  Number of miles driven already.
   * @param int $warrantyProvided
   *   Number of miles of warranty provided with the vehicle.
   */
  function __construct($color, $milesDriven = 0, $warranty = 100000) {
    $this->color = $color;
    $this->milesDriven = $milesDriven;
    $this->warranty = $warranty;
  }
 
  /**
   * Returns miles of warranty left on the vehicle.
   *
   * @return int
   *   Miles of warrnty left on the vehicle.
   */
  public function getWarrantyLeft() {
    if ($this->warranty - $this->getMilesDriven() > 0) {
      return ($this->warranty - $this->getMilesDriven());
    }
 
    return 0;
  }
 
  /**
   * Drive the vehicle. This will add to miles driven.
   *
   * @param int $miles
   *   Number of miles driven in the current trip.
   */
  public function drive($miles) {
    if ($miles > 0) {
      $this->milesDriven += $miles;
    }
  }
 
  /**
   * Returns the number of miles driven in total.
   *
   * @param int
   *   Total number of miles driven.
   */
  public function getMilesDriven() {
    return $this->milesDriven;
  }
}
 
/**
 * HondaAccord class.
 */
class HondaAccord extends Vehicle {
 
  // Stuff in the trunk.
  public $stuff;
 
  /**
   * Put stuff in trunk.
   *
   * @param mixed $stuff
   *   Stuff to be put in the trunk.
   */
  public function putInTrunk($stuff) {
    $this->stuff = $stuff;
  }
 
  /**
   * Take stuff out from the trunk.
   *
   * @return mixed
   *   Stuff returned from the trunk.
   */
  public function TakeFromTrunk() {
    $stuff = $this->stuff;
    unset($this->stuff);
    return $stuff;
  }
}
 
/**
 * ChevyRam class.
 */
class ChevyRam extends Vehicle {
 
  /**
   * Returns miles of warranty left on the vehicle.
   *
   * @return int
   *   Miles of warrnty left on the vehicle.
   */
  public function getWarrantyLeft() {
    if ($this->warranty - $this->getMilesDriven() > 0) {
      return (2 * ($this->warranty - $this->getMilesDriven()));
    }
 
    return 0;
  }
 
}
 
?>

Person.php

<?php
 
require_once 'Vehicle.php';
 
/**
 * Person class.
 */
class Person {
 
  // Vehicle
  public $vehicle;
 
  /**
   * Default constructor.
   */
  function __construct() {
    $this->vehicle = new HondaAccord('red');
  }
 
  /**
   * Let the person travel in his vehicle.
   *
   * param int $miles
   *   Number of miles that the person travels.
   */
  public function travel($miles) {
    $this->vehicle->drive($miles);
  }
 
  /**
   * Returns the number of miles that the person has traveled.
   *
   * @return int
   *   Number of miles that the person has traveled.
   */
  public function getDistanceTraveled() {
    return $this->vehicle->getMilesDriven();
  }
}
 
$you = new Person();
$you->travel(2000);
echo 'Distance traveled: ' . $you->getDistanceTraveled() . "\n";
 
?>
Following is the output when executing Person.php file:
$ php Person.php
Distance traveled: 2000

 

You will notice that in the constructor of Person class, we are initializing the vehicle he owns to be a red Honda Accord. So this code works fine as long as we expect all the people to own a red Honda Accord. But obviously this is not the case in real life

 

View More 1501
View More 1502
View More 1503
View More 1504
View More 1505
View More 1506
View More 1507
View More 1508
View More 1509
View More 1510
View More 1511
View More 1512
View More 1513
View More 1514
View More 1515
View More 1516
View More 1517
View More 1518
View More 1519
View More 1520
View More 1521
View More 1522
View More 1523
View More 1524
View More 1525
View More 1526
View More 1527
View More 1528
View More 1529
View More 1530
View More 1531
View More 1532
View More 1533
View More 1534
View More 1535
View More 1536
View More 1537
View More 1538
View More 1539
View More 1540
View More 1541
View More 1542
View More 1543
View More 1544
View More 1545
View More 1546
View More 1547
View More 1548
View More 1549
View More 1550
View More 1551
View More 1552
View More 1553
View More 1554
View More 1555
View More 1556
View More 1557
View More 1558
View More 1559
View More 1560
View More 1561
View More 1562
View More 1563
View More 1564
View More 1565
View More 1566
View More 1567
View More 1568
View More 1569
View More 1570
View More 1571
View More 1572
View More 1573
View More 1574
View More 1575
View More 1576
View More 1577
View More 1578
View More 1579
View More 1580
View More 1581
View More 1582
View More 1583
View More 1584
View More 1585
View More 1586
View More 1587
View More 1588
View More 1589
View More 1590
View More 1591
View More 1592
View More 1593
View More 1594
View More 1595
View More 1596
View More 1597
View More 1598
View More 1599
View More 1600
View More 1601
View More 1602
View More 1603
View More 1604
View More 1605
View More 1606
View More 1607
View More 1608
View More 1609
View More 1610
View More 1611
View More 1612
View More 1613
View More 1614
View More 1615
View More 1616
View More 1617
View More 1618
View More 1619
View More 1620
View More 1621
View More 1622
View More 1623
View More 1624
View More 1625
View More 1626
View More 1627
View More 1628
View More 1629
View More 1630
View More 1631
View More 1632
View More 1633
View More 1634
View More 1635
View More 1636
View More 1637
View More 1638
View More 1639
View More 1640
View More 1641
View More 1642
View More 1643
View More 1644
View More 1645
View More 1646
View More 1647
View More 1648
View More 1649
View More 1650
View More 1651
View More 1652
View More 1653
View More 1654
View More 1655
View More 1656
View More 1657
View More 1658
View More 1659
View More 1660
View More 1661
View More 1662
View More 1663
View More 1664
View More 1665
View More 1666
View More 1667
View More 1668
View More 1669
View More 1670
View More 1671
View More 1672
View More 1673
View More 1674
View More 1675
View More 1676
View More 1677
View More 1678
View More 1679
View More 1680
View More 1681
View More 1682
View More 1683
View More 1684
View More 1685
View More 1686
View More 1687
View More 1688
View More 1689
View More 1690
View More 1691
View More 1692
View More 1693
View More 1694
View More 1695
View More 1696
View More 1697
View More 1698
View More 1699
View More 1700
View More 1701
View More 1702
View More 1703
View More 1704
View More 1705
View More 1706
View More 1707
View More 1708
View More 1709
View More 1710
View More 1711
View More 1712
View More 1713
View More 1714
View More 1715
View More 1716
View More 1717
View More 1718
View More 1719
View More 1720
View More 1721
View More 1722
View More 1723
View More 1724
View More 1725
View More 1726
View More 1727
View More 1728
View More 1729
View More 1730
View More 1731
View More 1732
View More 1733
View More 1734
View More 1735
View More 1736
View More 1737
View More 1738
View More 1739
View More 1740
View More 1741
View More 1742
View More 1743
View More 1744
View More 1745
View More 1746
View More 1747
View More 1748
View More 1749
View More 1750
View More 1751
View More 1752
View More 1753
View More 1754
View More 1755
View More 1756
View More 1757
View More 1758
View More 1759
View More 1760
View More 1761
View More 1762
View More 1763
View More 1764
View More 1765
View More 1766
View More 1767
View More 1768
View More 1769
View More 1770
View More 1771
View More 1772
View More 1773
View More 1774
View More 1775
View More 1776
View More 1777
View More 1778
View More 1779
View More 1780
View More 1781
View More 1782
View More 1783
View More 1784
View More 1785
View More 1786
View More 1787
View More 1788
View More 1789
View More 1790
View More 1791
View More 1792
View More 1793
View More 1794
View More 1795
View More 1796
View More 1797
View More 1798
View More 1799
View More 1800
View More 1801
View More 1802
View More 1803
View More 1804
View More 1805
View More 1806
View More 1807
View More 1808
View More 1809
View More 1810
View More 1811
View More 1812
View More 1813
View More 1814
View More 1815
View More 1816
View More 1817
View More 1818
View More 1819
View More 1820
View More 1821
View More 1822
View More 1823
View More 1824
View More 1825
View More 1826
View More 1827
View More 1828
View More 1829
View More 1830
View More 1831
View More 1832
View More 1833
View More 1834
View More 1835
View More 1836
View More 1837
View More 1838
View More 1839
View More 1840
View More 1841
View More 1842
View More 1843
View More 1844
View More 1845
View More 1846
View More 1847
View More 1848
View More 1849
View More 1850
View More 1851
View More 1852
View More 1853
View More 1854
View More 1855
View More 1856
View More 1857
View More 1858
View More 1859
View More 1860
View More 1861
View More 1862
View More 1863
View More 1864
View More 1865
View More 1866
View More 1867
View More 1868
View More 1869
View More 1870
View More 1871
View More 1872
View More 1873
View More 1874
View More 1875
View More 1876
View More 1877
View More 1878
View More 1879
View More 1880
View More 1881
View More 1882
View More 1883
View More 1884
View More 1885
View More 1886
View More 1887
View More 1888
View More 1889
View More 1890
View More 1891
View More 1892
View More 1893
View More 1894
View More 1895
View More 1896
View More 1897
View More 1898
View More 1899
View More 1900
View More 1901
View More 1902
View More 1903
View More 1904
View More 1905
View More 1906
View More 1907
View More 1908
View More 1909
View More 1910
View More 1911
View More 1912
View More 1913
View More 1914
View More 1915
View More 1916
View More 1917
View More 1918
View More 1919
View More 1920
View More 1921
View More 1922
View More 1923
View More 1924
View More 1925
View More 1926
View More 1927
View More 1928
View More 1929
View More 1930
View More 1931
View More 1932
View More 1933
View More 1934
View More 1935
View More 1936
View More 1937
View More 1938
View More 1939
View More 1940
View More 1941
View More 1942
View More 1943
View More 1944
View More 1945
View More 1946
View More 1947
View More 1948
View More 1949
View More 1950
View More 1951
View More 1952
View More 1953
View More 1954
View More 1955
View More 1956
View More 1957
View More 1958
View More 1959
View More 1960
View More 1961
View More 1962
View More 1963
View More 1964
View More 1965
View More 1966
View More 1967
View More 1968
View More 1969
View More 1970
View More 1971
View More 1972
View More 1973
View More 1974
View More 1975
View More 1976
View More 1977
View More 1978
View More 1979
View More 1980
View More 1981
View More 1982
View More 1983
View More 1984
View More 1985
View More 1986
View More 1987
View More 1988
View More 1989
View More 1990
View More 1991
View More 1992
View More 1993
View More 1994
View More 1995
View More 1996
View More 1997
View More 1998
View More 1999
View More 2000

Related Articles

Leave a Reply

Back to top button