PHP Object Oriented Programming

PHP is a server side scripting language, mainly used for web development but also used as a general purpose programming language. Object Oriented Programming (PHP OOP), is a type of programming language principle added to php5, which helps in building complex and reusable web applications.

Object Oriented concepts in PHP are:

  • Class – This is a data type defined by the programmer, which includes local functions as well as local data. You can think of a class as a template for creating multiple instances of the same type (or class) of objects.
  • Object – An individual instance of a data structure defined by a class. You define a class once and then create multiple objects to it. Objects are also known as instances.
  • Inheritance – When a class is defined by inheriting existing functions from parent class then it is called inheritance. Here the child class will inherit all or some of the functions and member variables of the parent class.
  • Polymorphism – This is an object-oriented concept where the same function can be used for different purposes. For example the function name will stay the same but take a different number of arguments and can perform different tasks.
  • Overloading – a type of polymorphism in which some or all operators have different implementations depending on their argument types. Similar functionality can also be loaded with different implementations.
  • Data Abstraction – Any representation of data that hides its implementation details (abstract). * Encapsulation – refers to the concept where we encapsulate all the data and member functions together to form an object.
  • Constructor – refers to a special type of function that will be called automatically whenever an object is created from the class.
  • Destructor – refers to a special type of function that will be called automatically whenever an object is deleted or goes out of scope.

Classes & Objects:

A class is a programmer-defined data type, which includes local methods and local variables. This is a collection of objects. Objects have properties and behaviors.

First we have to define the php class, where the class name must be the same as the file name.

Example for a simple class:

<?php

class Books{

  public function name(){

  echo “Drupal book”;

  }

  public function price(){

  echo “900 Rs/-“;

  }

}

//To create php object we have to use a  new operator. Here php object is the object of the Books Class.

$obj = new Books();

$obj->name();

$obj->price();

?>

Output:

Drupal book

900 Rs/-

Creating Objects in PHP

When a class is created, we can create any number of objects to that class. The object is created with the help of the new keyword.

Call Member Function

When object is created we can access variables and functions of class methods with the help of ‘-> operator, accessing methods is done to get information from that method. Also see how we can access object properties via variables

<?php

   class Mobile {

      /* Member variables */

      var $price;

      var $title;

      /* Member functions */

      function setPrice($par){

         $this->price = $par;

      }

      function getPrice(){

         echo $this->price .”

“;

      }

      function setName($par){

         $this->title = $par;

      }

      function getName(){

         echo $this->title .”

“;

      }

   }

$Samsung = new Mobile();

$Xiaomi = new Mobile();

$Iphone = new Mobile();

$Samsung->setName( “SamsungS8 );

$Iphone->setName( “Iphone7s” );

$Xiaomi->setName( “MI4” );

$Samsung->setPrice( 90000 );

$Iphone->setPrice( 65000 );

$Xiaomi->setPrice( 15000 );

Now you call another member functions to get the values set by in above example

$Samsung->getName();

$Iphone->getName();

$Xiaomi->getName();

$Samsung->getPrice();

$Iphone->getPrice();

$Xiaomi->getPrice();

?>

Output

Samsung S8

Iphone S7

MI4

90000

65000

15000

Legacy

When the properties and methods of a parent class are accessed by a child class, we call the concept of having inheritance. Child classes can inherit parent methods and provide their own method implementation, this property is called overridden method. When the same method of the parent class is inherited, we call the inherited method. Now let’s take a look at the inheritance types supported in Object Oriented Programming and the corresponding Php inheritance examples.

Inheritance Type

  • Single Tier Inheritance
  • Graded Inheritance

Single Tier Inheritance:

In Single Level Inheritance Parent class method will be extended by child class. All methods can be inherited.

Single Tier Inheritance Example

<?php

class A {

    public function printItem($string) {

        echo ‘Hi : ‘ . $string . “\n;

    }

    public function printPHP() {

        echo ‘I am from valuebound’ . PHP_EOL;

    }

}

class B extends A {

    public function printItem($string) {

        echo ‘Hi: ‘ . $string . PHP_EOL;

    }

 public function printPHP() {

        echo “I am from ABC”;

    }

}

$a = new A();

$b = new B();

$a->printItem(‘Raju’);

$a->printPHP();      

$b->printItem(‘savan’);

$b->printPHP();      

?>

Output

Hi : Pavan I am from valuebound Hi: savan I am from ABC

Graded Inheritance:

In MultiLevel Inheritance, parent class methods will be inherited by child class and subclass will inherit child class methods.

<?php

class A {

            public function myage() {

            return  “age is 80\n”;

            }

}

class B extends A {

            public function mysonage() {

            return “age is 50\n”;

            }

}

class C extends B {

            public function mygrandsonage() {

            return  “age is 20\n”;

               }

        public function myHistory() {

        echo “Class A ” .$this->myage();

        echo “Class B “.$this-> mysonage();

        echo “Class C ” . $this->mygrandsonage();

        }

}

$obj = new C();

$obj->myHistory();

?>

Output

Class A age is 80

Class B age is 50

Class C age is 20