Uncategorized

What is Object Oriented Programming?

The central concept in the undestanding of Object Oriented Programming is the concept of Object. An Object is a group of data (called properties) and functions (called methods) that go together. Let’s consider a real-life example. A vehicle is an object. It can be defined to have three properties: color, miles driven and warranty (number of miles). It can be defined to have one method: getWarrantyLeft(). This method returns the number of miles of warranty left on the vehicle. Let’s create a template for such a Vehicle object. Open a file Vehicle.php and write the following:

<?php

/**
 * Vehicle class.
 */
class Vehicle {

  // Color of the vehicle.
  var $color = 'red'

  // Miles driven.
  var $milesDriven = 0;

  // Warranty available.
  var $warranty = 100000;

  /**
   * Returns miles of warranty left on the vehicle.
   *
   * @return int
   *   Miles of warrnty left on the vehicle.
   */
  function getWarrantyLeft() {
    if ($this->warranty - $this->milesDriven > 0) {
      return ($this->warranty - $this->milesDriven);
    }

    return 0;
  }
}

?>

Ignore the declaration $this in the code above for now. We’ll explain it later. What we have defined above is a template for a vehicle. Such a template is called Class. It’s not an object. The difference between Class and Object is similar to the difference between a person and you. Person is a common noun and there are billions of people roaming around in the world. On the other hand, there is only one of you. You have all the features of a person, but you are unique from every other person out there. If God were real and were to create another person, he’ll use the same Person template and create one more. That new person will still be different from you. In the same way, there will be multiple objects created from a class template. Your car and my truck, both are vehicles but they are different from each other. To create (or instantiate) an object from a class, you use new operator.

$yourCar = new Vehicle();
$myTruck = new Vehicle();

If you compare the variables $yourCar and $myTruck, you will get FALSE. Notice the operator ===. If you use == operator, you will see TRUE since == checks whether both the objects belong to the same class (which they do), but === checks whether the objects are identical or not.

// This will return FALSE.
echo ($yourCar === $myTruck);

Now let’s check what the method getWarrantyLeft() returns. When you are writing procedural code, you can write the following code at the end of Vehicle.php and it will work:

echo getWarrantyLeft();

But if you add the above code at the end of Vehicle.php and execute the file, you will get an error saying that getWarrantyLeft() is not defined. But you have defined this method then why is PHP complaining? The reason is that you have defined it inside the class Vehicle. For it to run, it needs to know which object it needs to run on because the number of miles of warranty left on my truck can be different from the number of miles of warranty left on your car. To get the number for each vehicle, we need to invoke it with the following syntax:

echo 'Warranty left on your car: ' . $yourCar->getWarrantyLeft() . "\n";
echo 'Warranty left on my truck: ' . $myTruck->getWarrantyLeft() . "\n";

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

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button