Skip to main content

Abstract Classes and Interface in PHP

Abstract class and Interface in PHP play very important role in oop. In this section, we will discuss the following point

  1. What are abstract classes?
  2. What is interface
  3. How to implement abstract classes in PHP
  4. How to implement interface in PHP
  5. Different between abstract classes and interface.

What is abstract Classes

As from name it seem like something that is hidden. Yes nature of the abstract classes are same. Abstract classes are those classes which can not be directly initialized. Or in other word we can say that you can not create object of abstract classes. Abstract classes always created for inheritance purpose. You can only inherit abstract class in your child class. Lots of people say that in abstract class at least your one method should be abstract. Abstract method are the method which is only defined but declared. This is not true definition as per my assumption. But your any class has at least one method abstract than your class is abstract class.

Usually, an abstract class is also known as base class. We call it base class because the abstract class is not the class which is available directly for creating an object. It can only act as the parent class of any normal class. You can use an abstract class in the class hierarchy. Mean one abstract class can inherit another abstract class also.

Abstract classes in PHP

Abstract classes in PHP are similar like other oop languages. You can create abstract classes in PHP using abstract keyword. Once you will make any class abstract in PHP you can not create an object of that class.
abstract class abc
{
public function xyz()
{
return 1;
}
}
$a = new abc();//this will throw error in php

above code will throw an error in PHP.

Abstract classes in PHP are only for inheriting in other class.
abstract class testParent
{
public function abc()
{
//body of your funciton
}
}
class testChild extends testParent
{
public function xyz()
{
//body of your function
}
}
$a = new testChild();

In above example, you are creating of testChild Class. TestChild class is inheriting testParent abstract class. So your abstract class is only available for inheritance. The main motive of creating abstract classes in PHP is to apply restriction of direct initialization or object creation.

Implementation of abstract method

As we know that abstract functions are those functions of abstract class which is only defined. It will be declared in your child class. You can create any method abstract using keyword abstract. You can only create abstract method either in abstract class or interface. Following is example of the abstract method implementation:

abstract class abc
{
abstract protected function f1($a , $b);
}
class xyz extends abc
{
protected function f1($name , $address)
{
echo "$name , $address";
}
}
$a = new xyz();

In class abc we have defined an abstract function f1. Now when we have inherited class abc then declared function f1. If you have an abstract method in your abstract class then once you inherit your abstract class then it is necessary to declare your abstract method. If you will not declare your abstract method then PHP will throw error in that case.

You can declare your abstract method in child class with the same visibility or less restricted visibility.

abstract class parentTest
{
abstract protected function f1();
abstract public function f2();
//abstract private function f3(); //this will trhow error
}
class childTest
{
public function f1()
{
//body of your function
}
public function f2()
{
//body of your function
}
protected function f3()
{
//body of your function
}
}
$a = new childTest();

In above code, you can see that you have declared 3 functions in the abstract class. But the private declaration of the abstract method will always throw an error. Because the private method is availabe only in the same class context. But in the case of f1. This is protected. Now in the child class, we have defined it as public because public is less restricted than protected. And for function f2 which is already public so we have defined it as public in our child class. We have defined it public because no any visibility is less restricted than public.

What is Interface?

Interface in oop enforce definition of some set of method in the class. By implementing interface you are forcing any class to must declaring some specific set of methods in oop. For example if you are creating class to render HTML element then it is necessary to set id and name of your html tag. So in this case you will create interface for that class and define method like setID and setName. So whenever someone will create any class to render HTML tag and implemented your interface then he must need to define setId and setName method in their class. In other word you can say that by help of interface you can set some definition of your object. Interface is very useful if you are creating architecture of any oop base application. Inter

Interface in PHP

Interface in php can be implemented like other oop lanugage. You can create interface in php using keyword interface. By implementation of interface in php class you are specifying set of the method which classes must implement.

You can create an interface in PHP using interface keyword. Rest of the things are typically identical to classes. Following is a very small example of an interface in PHP.
interface abc
{
public function xyz($b);
}

So in above code, you are creating an interface with name abc. Interface abc has function xyz. Whenever you will implement the abc interface in your class then you have to create a method with the name xyz. If you will not create function xyz then it will throw an error.

You can implement your interface in your class using implements keyword. Let us implement our interface abc in our class
class test implements abc
{
public function xyz($b)
{
//your function body
}
}

You can only define a method in the interface with public accessibility. If you will use other than public visibility in the interface then it will throw an error. Also while defining method in your interface does not use the abstract keyword in your methods.

You can also extend interface like class. You can extend the interface in PHP using extends keyword.
interface template1
{
public function f1();
}
interface template2 extends template1
{
public function f2();
}
class abc implements template2
{
public function f1()
{
//Your function body
}
public function f2()
{
//your function body
}
}

So here template2 has all property of tempate2. So whenever you will implement template2 in your class, you have to create function of both interfaces.

You can also extend multiple interfaces in one interface in PHP.
interface template1
{
public function f1();
}
interface template2
{
public function f2();
}
interface template3 extends template1, template2
{
public function f3();
}
class test implements template3
{
public function f1()
{
//your function body
}
public function f2()
{
//your function body
}
public function f3()
{
//your function body
}
}

You can also implement more than one interface in PHP class.
interface template1
{
public function f1();
}
interface template2
{
public function f2();
}
class test implments template1, template2
{
public function f1()
{
//your function body
}
public function f2()
{
//your function body
}
}

You can not implement 2 interfaces if both share function with the same name. It will throw an error.

Your function parameter in class must be identical to the parameter in the interface signature. Following is example some example
interface template1
{
public function f1($a)
}
class test implements template1
{
public function f1($a)
{
echo $a;
}
}

Above will work. But following example will not work:
interface template1
{
public function f1($a)
}
class test implements template1
{
public function f1()
{
echo $a;
}
}

But it is not necessary to use the same name of the variable. Like $a. You can also use any name. For example:
interface template1
{
public function f1($a)
}
class test implements template1
{
public function f1($name)
{
echo $name;
}
}

If you are using default argument then you can change your value of the argument. For example
interface template1
{
public function f1($a = 20)
}
class test implements template1
{
public function f1($name  = "ankur")
{
echo $name;
}
}

In above section, we have discussed interfaces and abstract classes in PHP. Both are almost doing same things but has some difference.

Differences between abstract class and interface in PHP

Following are some main difference between abstract classes and interface in PHP

  1. In abstract classes, this is not necessary that every method should be abstract. But in interface every method is abstract.
  2. Multiple and multilevel both type of inheritance is possible in the interface. But single and multilevel inheritance is possible in abstract classes.
  3. The method of PHP interface must be public only. A method in an abstract class in PHP could be public or protected both.
  4. In an abstract class, you can define as well as declare methods. But in the interface, you can only define your methods.

For more information about abstract classes and Interface in PHP you can read:
http://en.wikipedia.org/wiki/Interface_%28computing%29
http://php.net/manual/en/language.oop5.interfaces.php
http://php.net/manual/en/language.oop5.abstract.php

Next >> Overloading and Overriding in PHP

 

Support Me by Sharing This Article

Ankur Kumar Singh

I am a PHP programmer having some knowledge about Linux. I am always interested in web development and knowledge sharing. I am full time tech evangelist part time human being. :-)

69 thoughts on “Abstract Classes and Interface in PHP

  1. Why does every tutorial leave out this question

    If i have 3 classes of functionality – and a 4th class needs the functionality from the 3 other classes (class: Accounts, Logging, Permissions and the 4th class needs all 3 – how does one use interfaces to get to the code that already exists)

    1. PHP classes implement interface instead of inheriting it. Since protected is used for limiting access to the functions/properties of classes to its sub classes, public modifier is used.

  2. Also, you seem to have misunderstood the meaning of ‘definition’ and ‘declaration’, you are swapping its meaning. In abstract classes you ‘declare’ a method not ‘define’ it. Kindly get it corrected because you are unknowingly affecting other people’s understanding of this concepts.

  3. Those who follow this tutorial should be aware of the term ‘declare’ and ‘define’ being used by him here. This tutorial misplaced both!!!! otherwise it is good!

  4. you are swapping the meaning of defining and declaring…..please correct this mistake….

    defining means body of a function or method…
    like function add(){
    //code here
    }
    declaring means only method name like function add();

  5. Really nice tutorial,
    i understood both abstract classes and interface, bookmarked this tutorial for my further references.
    many thanks to author of this article

  6. your texting is quite good but i doubt about your examples because it doesnt prove me when I should use interfaces. I could easy “translate” your interface examples to class’ics. Your examples are more about how to use interfaces as alters to classes. Its like you would say that todays phones are used for sms and calling and you can tap screen to get letter or swap phone for reactions 🙂 You should mention that interfaces are used for objects/classes (especialy, diferent types like Human and, e.x., Animal) communication and when several diferent (type) classes have same functions like Plane ar Bird can fly.

  7. i think in case of abstract class we only declare functions in it , and defined it in child class.
    but here u have mentioned wrong

  8. Hi Thanks for this good tutorial, Can you please explain below point

    4. In abstract class you can define as well as declare methods. But in interface you can only defined your methods.
    As per your difference i tried below code but its not working. Can you please look into it.

    abstract class vehicle{
    abstract function move(){
    echo 420;
    }

    }

    class car extends vehicle{
    function move(){
    echo 220;
    }
    }
    $object = new car();
    $object->move();

    Thanks.
    Waiting for quick reply.
    Thanks again.

    1. As per abstract class property, It’s method can not hold body, only declaration of method should be there in abstract class, Implementation of method will be in inherited class.

      Finally you should have:

      abstract class vehicle{
      abstract function move();
      }

      class car extends vehicle{
      function move(){
      echo 220;
      }
      }
      $object = new car();
      $object->move();

  9. Your article is too good. But please read these definitions

    (Method Declaration: You are declaring that something exists, such as a class, function or variable. You don’t say anything about what that class or function looks like, you just say that it exists.)

    (Method Definition: You define how something is implemented, such as a class, function or variable, i.e. you say what it actually is.)

    In your article you have just swapped these meanings.

  10. Thank you Mr. Ankur Singh for explaining Abstract class and Interface in PHP, I should call this as a complete example for understanding Abstract class and Interface. Thanks again.

Leave a Reply

Your email address will not be published. Required fields are marked *

shares