Object Oriented Programming: - There is no constant definition of object oriented programming. We can understand is that in our surroundings everything is made of some objects. Our nature is made of trees, rivers, mountains, fountains etc. Just like a house is made of bricks, cement, wood etc. Similarly, Object Oriented Programming assumes everything as an object. This programming is known as oops. Oops was created with purpose of practical modelling in simula-67. The concept of oops to implement a software with using different objects. It treats function and data as a object, where keywords are objects. Oops is work on how data and functions represents as a form of reusable object.

There are some important terms which related to object oriented programming:
Class: -   A class contains the methods, properties and characteristics of the object. It defines the object. The class may include local functions local data type. We can say class is a programmer defined data type. It is the blueprint for the object. We can assume class as template to make many instance of the similar type of object and class. We can understand it by an example. We use different smart phones. All smart phones have same characteristics, like they have battery, Sim card, they are painted some colour, and they each have a price. All smart phones do similar things also, turn on, turn off, play games, play music, store data, capturing image etc. These can be described as functions. Same way in OOPs methods the class keeps definition, and object keeps the value. In PHP we declare class by using the class keyword.


Parent class: Parent class is inherited by another class. This is also called a primary class, super class or base class.
Child Class: Child class is inherited by another class. This is also called a subclass or derived class.

The syntax for defining a new class in PHP is as follows:
<--?php
class phpClass{
   var $var1;
   var $var2 = "constant string";
   function myfunc ($arg1, $arg2) {
      [..]
   }
   [..]
}
?-->

Description of each statement:
•    The special form class, followed by the name of the class that we want to define.
•    A set of braces {} enclosing the number of variable declarations and function definitions.
•    Variable declarations start with the special form var. It is followed by a conventional $ variable name; they also have an initial assignment for a constant value.
•    Functions definitions are local to the class and will be used to set and access object data. They look like standalone PHP functions.


Example:
 An example for defines a class of vehicle type:
<--?php
class  Vehicles{
    /* Member variables */
    var $colour;
    var $model;
    /* Member functions */
    function setcolour($par){
       $this->colour = $par;
    }
    function getcolour(){
       echo $this->colour;
    }
    function setmodel($par){
       $this->model = $par;
    }
    function getmodel(){
       echo $this->model;
    }
}
?-->


Here variable $this is a special variable. It refers to the same object.

Object:  In Oops objects are known as instances. The object is an individual instance of the data structure. It is defined by a class. We have to define a class only once and then can make many objects that belong to defined class. An object is a bunch of variables and functions. They all lumped into a single entity. An object contains the methods and properties. Here methods are functions that manipulate data within the object and the properties are variable those keeps information about the object. For creating new object from the class we have to use new keyword.
After defined a class, we can create as many objects like of declared class type. There is an example of how to create object by using new operator.
$honda = new vehicles;
$hyudai = new vehicles;


In above case we have created two objects and these objects are independent of each other.  Both of two have their existence separately. Here we will know how to access member function and process member variables.
Calling Member Functions: - Member Functions are defined inside the class. These are using for accessing object data. After creating objects for class, we are able to call member functions related to objects and one and other member function is able to process member variable and its related object.
Following example shows how to set model and colour for the two vehicles by calling member functions.
   $honda->setModel( "Amaze" );
   $hyundai->setModel( "Santro" );
   

   $honda->setcolour( white );
   $hyundai->setcolour( silver );


Now we call another member functions to get the values set by in above example:
   $honda->getmodel();
   $hyundai->getmodel();
   
   $honda->getcolour();
   $hyundai->getcolour();


The output will be as follow:-
  Amaze
  Santro
  White
  Silver

Public: It is a property or permission that accessible from anywhere in the application. Here all class members are public default.

Protected: It is a property or permission that accessible only from a class. It extends from the parent class.
Private: It is a property or permission that accessible only within the current working class.

Inheritance: Inheritance is the greatest feature of Object Oriented Programming. In PHP when a class is defined by optionally inheriting existing function of a parent class using extends clause. It is called inheritance. It has the ability to extend the child class and inherit the characteristics of parent class. In inheritance child class will inherit the variables of a parent class and all or few member functions. The output of an extend class has characteristics of the parent class, whatever is in the new child, or extended class.

The syntax of inheritance is as follows:
class Child extends Parent {
     --definition body--
  }


The effect of inheritance is that the child class, subclass or derived class has the following characteristics:
•    It has all the member variable declarations of the parent class automatically.
•    It has all the same member functions as the parent. They will work the same way by default as those functions do in the parent.
This example inherits vehicles class and adds more functionality based on the requirement.
class Wheelers extends Vehicless{
   var manufaturer;
   function setManufacturer($par){
     $this->manufacturer = $par;
   }
   function getManufacturer(){
     echo $this->manufacturer;
   }
}

Class wheelers keeps two additional member functions apart from inherited functions.

Overloading: Overloading is a kind of polymorphism. It works on Function definitions in the child class and override definitions respect to same name in the parent classes. Here in child class, we can also do modify the definition of a function inherited from parent class. In overloading all kind of functions are overloaded with different implementation. Similarly, some or all of operators can be overloaded which has different creation and implementations that are depending on the types of their arguments. The overloading refers to the property and ability to able to get and set objects properties. While __set() is called when we change that properties value.

In this following example getColour and getModel functions are overridden for return values.

function getcolour(){
       echo $this->color;
       return $this->color;
    }
    function getmodel(){
       echo $this->model;
       return $this->model;
    }

If you face any difficulty to understand this then feel free to contact me through below contact form.
Don't Forget to send your feedback. Thanks

0 comments :

Post a Comment

 
# Top