PHP

Encapsulation, Polymorphism and Inheritance

PHP provides features to implement an object oriented model. These features includes encapsulation, inheritance and polymorphism.

Encapsulation or information hiding

Encapsulation is just wrapping some data in an object. The term “encapsulation” is often used interchangeably with “information hiding”.

Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state.

You can use encapsulation if the properties of an object are private, and the only way to update them is through public methods.

For example in this code we can protect our User objects from setting an invalid gender parameter.

class User {
 private $name;
 private $gender;

 public function getName() {
  return $this->name;
 }

 public function setName($name) {
  $this->name = $name;
  return $this;
 }

 public function getGender() {
  return $this->gender;
 }

 public function setGender($gender) {
  if ('male' !== $gender and 'female' !== $gender) {
   throw new Exception('Set male or female for gender');
  }
  $this->gender = $gender;
  return $this;
 }
}

$user = new User();
$user->setName('Michal');
$user->setGender('male');

In above example we should assign male or female value for setGender method or it will print Fatal error: Uncaught exception 'Exception' with message 'Set male or female for gender'.

Polymorphism

Ability to process objects differently. An integral part of polymorphism is the common interface.

Polymorphism is a design pattern in which classes have different functionality while sharing a common interface. You can define an interface in PHP using interface or abstract classes.

This example will describe the general concept of polymorphism, and how it can easily be deployed in PHP.

In the example given below, the interface with the name of Shape commits all the classes that implement it to define an abstract method with the name of area().

Step 1 : declaring interface using interface keyword.

interface Shape {
}

Step 2: add method (methods can be defined in the interface without the body).

interface Shape {
 public function area();
}

Step 3: create Circle class that implements the Shape interface by putting into the area() method the formula that calculates the area of circles:

class Circle implements Shape {
 private $radius; 
 
 public function __construct($radius) {
 $this -> radius = $radius;
 }
 
 public function area() {
 return $this -> radius * $this -> radius * pi();
 }
}

Step 4: The rectangle class also implements the Shape interface but defines the method area() with a calculation formula that is suitable for rectangles:

class Rectangle implements Shape {
 private $width; 
 private $height;
 
 public function __construct($width,$height) {
 $this -> width  = $width;
 $this -> height = $height;
 }
 
 public function area() {
 return $this -> width * $this -> height;
 }
}

By implementing interface we can be sure that all of the objects calculate the area with the method that has the name of area(), whether it is a rectangle object or a circle object (or any other shape).

$rect = new Rectangle(3,3);
echo $rect -> area();

$circ = new Circle (5);
echo $circ -> area();

Inheritance

Take properties of existing objects

Since a class is simply a collection of related functions and variables, one way of adding new functionality is to amend the code. In the early stages of development, this is usually the correct approach, but a fundamental aim of OOP is reusability and reliability. There’s no need to code everything from scratch, you can base a new class on an existing one. OOP classes are extensible through inheritance.

A child class or subclass in OOP can inherit all the features of its parent or superclass, adapt some of them, override existing methods and properties, and add new ones of its own.

class Human {
 protected $name;
 
 public function __construct($name){
  $this->name = $name;
 }
}

In order to declare that one class inherits the code from another class, we use the extends keyword.

class Person extends Human {
 /* no constructor, adapted the parent constructor */
 public function getName(){
  /*Will return name property defined in parent class*/
  return $this->name; 
 }
}

The above code demonstrated that how Person class inherited the parent constructor and added new method as per our need.

//constructor function inherited from the parent
$person = new Person('Kelly');
echo $person->getName();
by BrainBellupdated
Advertisement: