Data Abstraction: Data abstraction is related to an abstract class, which means a class that cannot be instantiated on its own. The representation of data in this class and the implementation details are hidden. If you want to declare any class method to be abstract, you must have also declared the class itself as to be abstract. In data abstraction, it is a remarkable point that you can inherit from an abstract class. Any kind of class that extends an abstract parent class must create an interface of the parent abstract class includes its methods. Sometimes data abstraction can generate a fatal error. If this do not generate any kind of error. This ensures that the implementation is correct and output will be also error free.

We can understand data abstraction by following example:
Where mathematics is a abstract class and other are child class
<--?php

abstract class mathematics{

/**
*
* Add two to a number
*
* @access public
*
* @return int
*
**/
public function addTwo(){
  return $this->num+2;
}

} /*** end of class ***/

/*** try to create new object from the mathematics class ***/
$math = new mathematics

?--> 


Constructor: Advance PHP provides special function to Oops PHP called __construct() in Oops to define a constructor . These Constructor Functions are referring to a special type of functions. They are called automatically whenever there is an object created. So we can take full advantage of its behavior, by initializing many things through constructor functions.
In following example we will create one constructor for vehicles class and it will initialize model and colour for the vehicle at the time of object creation.
function __construct( $par1, $par2 ){
   $this->model = $par1;
   $this->colour = $par2;
}


After that we don't have need to call set function separately for setting model and colour. We can initialize these two member variables at the time of object creation only. We can understand by it following example:
$honda = new vehicles( "Amaze", white );
   $hyundai = new vehicles ( "Santro", silver );
   

   /* Get those set values */
   $honda->getModel();
   $hyundai->getModel();
   $honda->getPrice();
   $hyundai->getPrice();


The output will be as follow:
  Amaze
  Santro
  White
  Silver
Destructors: - We can define destructor function using function __destruct(). These destructors functions are refers for the special kind of function.  We can release all the resources with-in a destructor by using destruct() function.

Interfaces: - The interfaces allow us to define a common structure for classes’ declaration. In PHP an interface cannot be instantiated on its own. As we know in all programming language Oops is re-use of code. Interfaces make this process is much easier. The interface methods have no any internal logic, they are simply a "mapping" or constraint of what kind of class or classes should be implementing. We can create multiple classes to implement with interface. These interfaces classes will not inherit from each other. When we inherit from a parent class, we have to may choose to override. If you had multiple parent class methods with the same name, but all have different functionality or characteristics, sometimes PHP would have confuse to tell which method is to be use. In PHP, classes are implementing an interface, these must implement every method. That’s why multiple inheritances do not work in PHP.

In following example we see our vehicle class from earlier, with the addition of a stop() function:
<--?php
class vehicle implements testdrive{

/*** define public properties ***/

/*** the color of the vehicle ***/
public $color;

/*** the number of doors ***/
public $num_doors;

/*** the price of the vehicle ***/
public $price;

/*** the shape of the vehicle ***/
public $shape;

/*** the brand of vehicle ***/
public $brand;

/*** the constructor ***/
public function __construct(){
  echo 'About this Vehicle.';
}

/*** define some public methods ***/

/*** a method to show the vehicle price ***/
public function showPrice(){
  echo 'This vehicle costs '.$this->price.'.';
}

/*** a method to show the number of doors ***/
public function numDoors(){
  echo 'This vehicle has '.$this->num_doors.' doors.';
}

/*** method to drive the vehicle ***/
public function drive(){
  echo 'VRRROOOOOOM!!!';
}
/**
* a method to stop the car
*
* @access public
*
**/
public function stop(){
  echo 'SSSCCRRREEEEEECCHH!!!';
}

} /*** end of class ***/

/*** declare our interface ***/
interface testdrive{
/*** some functions that must be implemented ***/
function drive();
function stop();
}

/*** an new vehicle object ***/
$object = new vehicle;

/*** call some methods ***/
$object->drive();
$object->stop();
?--> 


Polymorphism: Polymorphism is an object oriented concept .The word polymorphism is derived from two words. One is Poly and other is Morph. These are words of Greek language where poly means MANY and Morph means FORM.  So polymorphism is work as many forms. In polymorphism method we can use same functions for different purposes. In oops under polymorphism a function makes different number of arguments but function name will be remain same. Polymorphism can do different task also. In oops we can invoke the correct method by passing the correct method. Polymorphism works on to invoke a function call that is made by inspecting the object.
Syntax for polymorphism programming:
Class Baseclass {
     Public function mymethod() {
          Echo “BaseClass method called”;
          }
         }
       Class DerivedClass extends BaseClass {   
            Public function mymethod() {
                  Echo “DerivedClass method called”;
                  }
         }


We can understand by it following example:
class Honda {
    var $name;
    function __construct($name) {
        $this->name = $name;
    }
}

class Passion extends Honda {
    function wheels() {
        return "two wheeler!";
    }

class Amaze extends Honda {
    function wheels() {
        return "four wheeler";
    }
}


Encapsulation: This method is like enclosing in a capsule. Encapsulation refers to a concept where we encapsulate all the data, related operations, and member functions together to form an object. Simply encapsulation works on wrapping up data members and member function together into a single component. Encapsulation hides the internal details of the object. It prevents the client and user from how an object does work and it’s inside view. Encapsulation is a technique that implemented the behavior of abstraction and protects the information of a object from other objects. Encapsulation also protects the data code from accidental corruption by making an impenetrable wall. The features of encapsulation are supported for classes which are using in object oriented programming. Sometimes it is also possible in non- object oriented language. In PHP encapsulation hide the data for purpose such as making the variable as private which would be public as well as expose the property to access the private data.
We can understand encapsulation by following Example:
	<--?php
	Class YourMarks
	{
	Private $marks;
	Public marks
	{
	Get {return $mark; }
	Set { if ($mark>0) $mark = 20; else $mark = 0; }
	}
	}
	?-->

Thanks and Happy Coding. Don't forget to give feedback.

0 comments :

Post a Comment

 
# Top