PHP: Object-Oriented Prorgamming


Overview

Object-oriented programming (OOP) is a method of programming that involves the following:

  • Programs are organized around "objects" and data, not just the process. Objects represent real-world things. Objects have "attributes" and "behaviors".
  • A "class" is a definition of objects of the same kind. It is a blueprint, template, or prototype that defines and describes the static attributes and behaviors common to all objects of the same kind.
    • person class: attributes (name, height, weight, gender, age); behaviors (speak, walk, sleep). In other words, attributes are the "data", behavior is "functions".
    • Many languages use the terms properties" to describe "attributes" and "methods" to describe "behaviors".
    • Classes describe things in "abstract" terms. Class is just the description, not the actual person or persons name.
    • Inheritance − When a class is defined by inheriting existing function of a parent class then it is called inheritance. Here child class will inherit all or few member functions and variables of a parent class.
    • Parent class − A class that is inherited from by another class. Also known as base class or super class.
    • Child Class − A class that inherits from another class. Also known as subclass or derived class.
  • An "instance" is a realization of a particular item of a class. An instance is an instantiation of a class. All the instances of a class have similar properties, as described in the class definition.
    • For example, you can define a class called "Employee" and create three instances of the class "Employee" for "Mark Smith" and "Mary Jones".
    • The term "object" usually refers to instance. An object, or multiple objectis, are created from a class.
  • Relationships can be created between objects, e.g. one object can inherit characteristics from another object.
  • Encapsuation - classes are self-contained units that reprepsent both the data and the code that works on that data: the properties and methods (bundling data and methods).
  • Benefits:
    1. Modularity
    2. Information-hiding
    3. Code re-use ("Don't Repeat Yourself" - DRY)
    4. Pluggability and debugging ease
    5. Easy to maintain code.
  • Constructor − special type of function which will be called automatically whenever there is an object formation from a class.
  • Destructor − special type of function which will be called automatically whenever an object is deleted or goes out of scope.

    Example: Basic OOP in PHP

    
    	<?php
    	  class Car 
    	  {
    		  // Member variables
    		  var $price;
    		  
    		  // Member functions 
    		  function setModel($par){
    			 $this->model = $par;
    		  }
    		  
    		  function getModel(){
    			 echo "<p>Model: " . $this->model . "</p>";
    		  }
    
    		  function setPrice($par){
    			 $this->price = $par;
    		  }
    		  
    		  function getPrice(){
    			 echo "<p>Price: $" . $this->price . ".</p>";
    		  }	  
    	  }
    	  
    	  // Create objects
    	  $grandcher2018 = new Car;
    
    	  // Call member functions
    	  $grandcher2018->setModel( "Jeep Grand Cherokee" );
    	  $grandcher2018->setPrice( 30695 );
    	  
    	  $grandcher2018->getModel();
    	  $grandcher2018->getPrice();
    
    	?>
    	

    Output:

      Model: Jeep Grand Cherokee

      Price: $30695.

    Run it now