Understand the concept of Interface in PHP

What is Interface?

An Interface contains methods without body i.e {}. Means, there we get only the declaration of the function, not the definition of the function.

A class which uses interface have to implement all of its methods declared in the interface.

Interface restricts the user to define all the method declared in the Interface in a class, which uses the interface.

How can we define an Interface?

In PHP we can define an Interface using interface keyword before the interface name as we defined below

interface MyFirstInterface{
// required code
}

Interface can have only Abstract Methods and Constant Variables

interface MyFirstInterface{
 const CONSTANT1 = 25; // contant variable
const CONSTANT2 = 50; // constant variable
public function method1();   // Abstract Method
}

How can we use Interface?

The interface can be used by the class using implements keyword.

public class MyClass implements MyFirstInterface{
   public function method1(){
// body of method1
}
}
public class MyClass implements MyFirstInterface, MySecondInterface{  
public function method1(){ 
// body of method1
} 
}
// MyFirstInterface and MySecondInterface are interface here.

This how we can use multiple interfaces at once with a Class.

The interface can be used by another interface using extends keyword.

public interface MySecondInterface implements MyFirstInterface{   
        public function method2(); // abstract method;
}

Why Should we use Interface?

When you want some user who uses your code, should implement some method according to their need which will be customizable by the user. And you want user must implement specific methods. which will be needed by your code.

In these case, you need to define Interface which will restrict the user to implement these methods.

For example:

You are creating Article Publishing Workflow. So to restrict the user to implement compulsory methods in their ArticleWorkflow Class you will need to create an interface.

interface Workflow{
public function draft();
public function sendForReview();
public function sendForPublished();
}

This workflow can be used in Article Publishing Class or Image Publishing Class or Video Publishing Class etc. So Whenever you need to create a workflow you just need to implement this interface only, then this interface will decide the what method should be implemented while creating a workflow class. So by defining the workflow interface you just created a standard process to introduce a New Workflow in the system.

I hope you found this post useful and Happy Learning.

 

 

Concept of Inheritance in PHP

It is one of the important programming principle in OOPs Concepts. It will let you to code in a more modular way by which you will be able to reuse your code. It introduces the parent and child class concepts in programming language. To implement the inheritance in PHP we use “extends” keyword.

For example, we have two class

  1. Person
  2. Employee

Now let’s decide the relationship between these two classes.

Here Person Class is parent class and Employee is a child class

Person Class is parent class because it shows more primitive information as comare to Employee class.

For example

what a Person Class shows us:-

  1. Gender : male/female.
  2. Name.
  3. Address.
  4. color.
  5. height.

What a Employee Class shows us:-

  1. Gender : male/female
  2. Name
  3. Address
  4. color
  5. height
  6. employee id
  7. employee department
  8. employee salary

These two classes are just for example otherwise it will show much information.

So let’s take close look at employee class here Gender, Name, Address, Color, Height are common with person class. So why do we define it again in Employee, it’s not good programming practice. So what we will do in this case

We define an Employee Class with three attributes only as below mentioned:-

  1. employee id
  2. employee department
  3. employee salary

And others will be inherited from Person Class.

How can we implement inheritance in PHP

class Person{
public $name;
public $gender;
public $address;
public $height;
public $color;
public $race;
}
class Employee extends Person{
public $employee_id;
public $employee_dept;
public $employee_sal;
}
$employee = new Employee();
var_dump($employee); // try this;

Types of inheritance possible with PHP

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance

Single Inheritance :

Single Inheritance consists of a single parent class and will have one child class. This kind of relationship comes from single inheritance. Above mentioned relationship between Person and Employee class is also an example of Single Inheritance.

Multilevel Inheritance :

Multilevel inheritance will have a chain of single inheritance. For example, there is parent and child relationship between Person and Employee class, Now If we define a Manager class which will inherit the employee class. Then this type of inheritance will create a Multilevel Inheritance.

class Person{
public $name;
public $gender;
public $address;
public $height;
public $color;
public $race;
}
class Employee extends Person{
public $employee_id;
public $employee_dept;
public $employee_sal;
}
class Manager extends Employee{
public $department;
public $manager_id;
public $reporting_manager;
}
$mgr1 = new Manager();
var_dump($mgr1); // try this;

Hierarchical Inheritance :

Hierarchical inheritance consists of a single parent class and that parent class is inherited by multiple child class. For Example, there is a parent class named Person and we have two child class named Employee and Student. Both these class inherit Person class. This type of the relationship comes from hierarchical inheritance.

Try out yourself for Hierarchical Inheritance.

 

Inheritance in PHP
Inheritance in PHP

Note:

  1. Multiple Inheritance is not supported in PHP
  2. We cannot use extend keyword with Final Class.
  3. Inheritance represents the IS-A relationship. For Example “Employee is a Person”.
  4. we access parent class property inside child class using parent keyword.
  5. For Interfaces, we use implement keyword to inherit its property.

Inheritance facilitates the code reusability feature which makes it one of the most important principles of OOPs Concepts.

Concept of Encapsulation in PHP

PHP Encapsulation

Wrapping up of the data into a single unit is called Encapsulation in Object-Oriented Programming Languages. Many people get confused when someone asked them to explain it with some examples. This Concept will come in your way every time when you go for an interview.

So now let’s understand the concept of Encapsulation in OOPs in detail.

Meaning of

Wrapping up of the data into a single unit

It means defining Class Member Variables and Member function inside the class. So when we create a class and define some member variables and function inside the class it becomes a single unit which will be known by the class name together.

This small statement has a very deep meaning.

Why we need Encapsulation?

When we wrap our data into something then it hides details from the user who uses it.

For example: When we create an Object from a Class we don’t know what has been written inside the constructor. How it initializes the value of the member variable, How member function we use through object manipulates the value of the member variables or how it generates the output.

This is the main reason for which we use encapsulation so that unnecessary details should be hidden from users who use the class. To achieve this we often make Member Variables private, So that it cannot be accessed outside the class and if someone wants to access these variable, it should be through the GETTER and SETTER methods.

Example of Encapsulation:-

class Person{
private $name;
public $gender; 
public $address; 
public $height; 
public $color; 
public $race; 
public function __construct($name){ 
$this->race = "human"; 
$this->name = $name;
}
public function getName(){
return $this->name;
}
public function setName($name){
$this->name = $name;
}
}

So due to Encapsulation, we are able to achieve Data hiding in OOPs.

So finally the concept of Encapsulation in PHP is hiding internal information of an object to protect from the other object.

This is the simple example of encapsulation with the use of Person Class. But sometimes overuse of inheritance undermines encapsulation in Object Oriented Programmings which we need to take care.

Reference from Wikipedia:

Because inheritance exposes a subclass to details of its parent’s implementation, it’s often said that “inheritance breaks encapsulation”

Gang of Four, Design Patterns
In next articles, we will discuss Inheritance and their types supported by PHP.