09 - Object Oriented Concepts in PHP

Today in programming world, mostly languages support the concepts of object oriented. In object oriented approach we mostly follow the concept of classes, objects and properties in the system.

Classes and objects

Object can be anything including physical and conceptual. Like book, customer, employee etc are of physical type of objects and conceptual like account, software etc.

Class is a collection of similar types of objects and also includes attributes (properties of objects) and operations (methods) those are used to define actions that can be performed with the help of objects.

In object oriented approach, following main features are included:

1. Encapsulation

Encapsulation is a feature of object oriented approach, which indicates data hiding. We can hide data that we don’t want to make accessible to the outer world. Objects can be grouped into classes. Classes represent a set of objects that must have a certain common attributes and behaviours. All these attributes and behaviours are bounded in a class.

2. Abstraction

Abstraction is a feature of object oriented approach, in which we can provide accessibility of some specific variables and method for users according to their relevancy. For eg If we want to provide accessibility to employees to fetch personal details of theirs only, we can do it by using the concept of abstraction

3. Inheritance

Inheritance allows inheriting a class in other subclasses. A derived or subclass can inherit the attributes and methods of parent class. This makes our code reusable, which is the most important advantage of an object oriented approach. This concept creates relationship between parent and child classes.

In PHP when we inherit one class in another, we have to use the keyword extends.

Inheritance can be of following types:

  • Single Inheritance

When a class is inherited by only one class, is called single inheritance

  • Hierarchical Inheritance

In hierarchical inheritance one class can be inherited by more than one classes.

  • Multilevel Inheritance

In this type of inheritance one class can be inherited by another class, and another class can also be inherited by another class. Inheritance can be on multi levels.

  • Multiple Inheritance

In this type of inheritance one class can inherit more than one class. This type of inheritance is not found in PHP, but most other programming languages like C++ has this type of inheritance.

  • Hybrid Inheritance

In this inheritance more than type of inheritance found.

In the above diagram multilevel and hierarchical inheritance is present.

 

4. Polymorphism

In object oriented approach polymorphism represents different behaviour for the same operation. Polymorphism can be static and dynamic.

In static polymorphism more than one function having same name but different in (number of arguments, type of arguments and sequence of arguments), can be defined in the same class. It is also known as function overloading. In this process, at the compilation time compiler decides which function will be called.

(Note: PHP does not support function overloading)

Polymorphism can be dynamic also, in which functions with same function signature present in both parent and child class. In this process it will decided at the run time

Structure of a class in PHP

In PHP, the structure of a class looks as follows:

Class employee
{
    //class definition
}

In class definition attributes and methods are included. We can declare variables in class definition using keyword var.

Class employee
{
var $employee_name;
var $emp_salary;
}

We can define functions within class definition.

Class employee
{
var $employee_name;
var $emp_salary;
function operation1()
{
    //function body
}
function operation2($param1, $param2)
{
    //function body
}
}

Other important concept in object oriented approach is 

Constructors:

Constructors are special member functions of a class. A constructor is automatically called, when we create an object of a class. It is used to initialize the attributes of class. A constructor has some special characteristics:

It has the same name as that of the class.

A constructor has no return type, not even void.

Constructors cannot be overloaded in PHP. In other words, Like other programming languages we cannot declare more than one constructor within the same definition of a class.

A constructor can be declared as:

Class employee
{
function employee($param)
{
echo "Constructor is called for $param<br>";
}
}

After declaration of a class, we must create an instance of this class that is the object of class. We can declare instance of class using new keyword.

Example:

<?php
class employee
{
    function employee($param)
    {
    echo "Constructor is called for $param";
    echo "<br>";
    }
}
    $a=new employee("employee1");
    $b=new employee("employee2");
    $c=new employee("");
?>

The output of above code is:

Using class attributes:

<?php
class employee
{
    var $emp_name;
    var $emp_salary;
    function details($param1,$param2)
    {
    $emp_name=$param1;
    $emp_salary=$param2;
    echo "The name os employee is:$emp_name";
    echo "<br>";
    echo "The salary of employee is:$emp_salary";
    echo "<br>";
    }
}
    $a=new employee();                            //object declaration of class
    $a->details("Robin John",20000);    // function call through class object
?>

Inheritance in PHP

While implementing inheritance in PHP, we use extends keyword to specify inheritance.

Following is the syntax of implementing inheritance in PHP:

Class A
{
    var $attribute1;
    function operation1()
    {
    }
}
Class B extends A
{
var $attribute2;
function operation2()
{
}
}

For eg:

<?php
class A
{
    var $attribute1;
    function operation1($param1)
    {
    $attribute1=$param1;
    echo "In parent class";
    echo "<br>";
    echo "Value of attribute of parent class is $attribute1";
    echo "<br>";
    }
}
class B extends A
{
    var $attribute2;
    function operation2($param2)
    {
    echo "In child class";
    echo "<br>";
    $attribute2=$param2;
    echo "Value of attribute of child class is $attribute2";
    echo "<br>";
    }
}         
$b1=new B();
$b1->operation1(500);

The output of above code is:

Function overriding

Function overriding is a concept, in which we define same attribute or same methods in a derived class those are present in parent class also. By using this concept we can assign different value in the same attribute

Example:

<?php
class A
{
    var $attribute;
    function operation($param1)
    {
    $attribute=$param1;
    echo "In parent class";
    echo "<br>";
    echo "We have to use $attribute for client side scripting";
    echo "<br>";
    }
}
class B extends A
{
    var $attribute;
    function operation($param2)
    {
    $attribute=$param2;
    echo "<br>";
    echo "In child class";                  
    echo "<br>";
    echo "We can use $attribute for server side scripting";
    echo "<br>";    
    }
}
$a=new A();
$a->operation("Java script");                        // parent class operation method will be called
$b=new B();
$b->operation("PHP language");                 // Derived class operation method will be called
?>

Magic methods in OOPs

In PHP special methods constructors and destructors are found, that are called when we perform certain actions like: creating instance of a class.

_construct and _destruct are special magic methods in PHP.

<?php           
class A
{
  public $attribute ="I am in class A";      
  public function __construct()
  {
      echo 'The class "', __CLASS__, '" constructor is called<br />';
  }             
  public function setAttribute($newvalue)
  {
      $this->attribute = $newvalue;
  }             
  public function getAttribute()
  {
      return $this->attribute . "<br />";
  }
  public function __destruct()
  {
      echo 'The class "', __CLASS__, '" is destroyed now.<br />';
  }
}             
// Create a new object of class A
$obj = new A;
// Get the value of $attribute before calling setAttribute
echo $obj->getAttribute();
 $obj->setAttribute("Attribute of class is changed now");
// Get the value of $attribute after calling setAttribute
echo $obj->getAttribute();
?>

Output of above code is:

Access specifiers in PHP

Access specifiers are used to provide accessibility in classes for properties and methods. There are three access specifiers:

  1. Public

All the methods and properties those are public can be accessed anywhere within and outside from class.

  1. Protected

When a method or property is declared as protected, it can be accessed within the class and in its derived class only.

  1. Private

The method or property that is defined as private can be accessible only within the class, in which it is defined. 

 

Like us on Facebook