Abstract – Interface keywords in PHP

PHP & MySQL Course: Abstract – Interface keywords in PHP

In the previous lecture 9, we learnt about the inheritance and polymorphism.

And we also used these concepts in the examples to understand them.

Lecture 10:

In this lecture 10, we will discuss the more advanced topics of the PHP

programming. And these will help us in dynamic website development. Topics are:

  1. Abstract class in PHP
  2. Interface keyword in PHP
  3. Implements keyword in PHP

Abstract Class in PHP:

As you know now we are in advanced PHP programming. And we are learning

the concepts of the OOP in PHP programming.

We use abstract class to hide the data in the classes. And when we use the abstract

class so it’s mean that we can’t use or declare any object for the abstract classes.

We use the abstract class to inherit it with the child class. As you saw how we can

inherit the classes in PHP programming in our previous lectures. In this lecture you

will learn how you can inherit the abstract classes in PHP. We also use the same

keyword here as extends. Check the listing 1 for details about the abstract classes in PHP.

Listing 1: Abstract Class in PHP

<br /><br />&lt;?<br /><br />abstract class Student{<br /><br />function StudentInformation()<br /><br />{<br /><br />$record = $this-&gt;Records();<br /><br />return $record;<br /><br />}<br /><br />abstract function Records();<br /><br />}<br /><br />class Teacher extends Student{<br /><br />function Records(){<br /><br />return 200;<br /><br />}<br /><br />}<br /><br />$print = new Teacher;<br /><br />echo "Total Students are: ".$print-&gt;StudentInformation();<br /><br />echo "&lt;br&gt;";<br /><br />?&gt;<br /><br />

As you can see in the listing 1, we declared an abstract class named Student.

And then we inherit it with Teacher class. And to use the hidden data

of the abstract class, we declared an object of the Teacher class and then

called the function or method of the Student abstract class by the object of the Teacher class.

Use of Interface in PHP:

The interface is a keyword in PHP. And we use it with classes in PHP programming for

developing the dynamic websites. And the interfaces are purely abstracted classes in PHP.

And we use the implement keyword to use the interface with methods.

Listing 2: Interface in PHP

<br /><br />&lt;?<br /><br />interface Information{<br /><br />public function DisplayData();<br /><br />}<br /><br />class Records implements Information{<br /><br />public function DisplayData()<br /><br />{<br /><br />echo "&lt;b&gt;Website Name:&lt;/b&gt; http//:www.trustingeeks.com";<br /><br />}<br /><br />}<br /><br />$object = new Records;<br /><br />echo $object-&gt;DisplayData();<br /><br />?&gt;<br /><br />

As you can see in the Listing 1 & 2, these both programs are doing same work.

And we used some keywords which are common in both programs i.e extends.

But there is a pretty difference between the abstract class and interface.

Difference Between Abstract Class and Interface:

  1. As you know we use abstract keyword to define the abstract class and same like this we also use it for methods. The important thing is that it is not important every method should be abstract in an abstract class but in an interface it is necessary.
  2. There is a concept of multiple and multilevel inheritance in PHP. The multilevel and single inheritance is being used in abstract classes because it is possible.
  3. There are four access modifiers which we can use them for methods. As we can just use public access modifier in the case of interface and in abstract classes we can use other all with public too.
  4. In abstract we can declare and also define the methods. But in interface we can’t do it, because we can just declare it then we will defined it somewhere else.


In this lecture we learnt most important and useful topics about the OOP in PHP

web pages programming. These things are useful when we want to secure our data on the web pages.


student of computer Science , having interest and experience in C++,Java,Php,Html,Css,JavaScript, wordpress and android development