What is class abstract class and interface in php?

Introduction 

Before diving deep into the difference between abstract class and interface, you must understand one basic thing: these are two completely different classes that cannot be used as an alternative to one another.

Interface classes completely empty the shells while expecting child classes to implement everything for them. Abstract classes not only contain the common piece of information between the shells inside but also expect the child classes within to fill in the gaps.

Let us dive in a bit deeper to actually understand the difference minutely.

If you want to read about PHP Array questions then you can visit here. This will help you crack your PHP interviews.

Interface Class

As we already know, an interface is actually defined by an interface keyword where all the methods are abstract. In addition to this, all the methods declared in this type of class must be declared in public which reflects the true nature of an interface.

Let's help to demonstrate that with an example:

  1. interface Logger {  
  2.     public  
  3.     function execute[];  
  4. }  

As you can see above, in the interface, the method body is not defined. Only the name and the parameters are being defined. Now, let's move on to the abstract class.

Abstract Class

In PHP, an abstract class is one being partially implemented by any developer. It might contain at least one abstract method which is basically a method without any written code. It just contains the name and the parameters and has been marked as “abstract”.

By definition, an abstract class is simply a function definition whose purpose is to serve the programmer by telling them the method in question must be implemented in a child class.

Here is an example to demonstrate the abstract class:

  1. abstract class AbstractClass {  
  2.     abstract protected  
  3.     function getValue[];  
  4.     public  
  5.     function printOut[] {  
  6.         print $this - > getValue[].  
  7.         "\n";  
  8.     }  
  9. }  

Now that you have been acquainted with what is an abstract an interface class, its time to delve into their differences, step by step.

Here’s a table depicting the difference between abstract and interface class in PHP.

Interface Class

Abstract Class

Interface class supports multiple inheritance feature

Abstract class does not support multiple inheritances.

This does not contain a data member.

Abstract class does contain a data member.

The interface does not allow containers.

The abstract class supports containers.

An interface class only contains incomplete members which refer to the signature of the member.

Abstract class contains both incomplete[i.e. abstract] and complete members.

Since everything is assumed to be public, an interface class does not have access modifiers by default.

An abstract class can contain access modifiers within subs, functions, and properties.

Any member of an interface cannot be static.

Only a complete member of the abstract class can be static.

Conclusion 

Now that you have learned abstract class and interface differences, it will be much easier to implement them on your project. 

Abstract class:

  • Abstract class comes under partial abstraction.
  • Abstract classes can maintain abstract methods and non abstract methods.
  • In abstract classes, we can create the variables.
  • In abstract classes, we can use any access specifier.
  • By using 'extends' keyword we can access the abstract class features from derived class.
  • Multiple inheritance is not possible.

Interface:

  • Interface comes under fully abstraction.
  • Interfaces can maintain only abstract methods.
  • In interfaces, we can't create the variables.
  • In interface, we can use only public access specifier.
  • By using 'implement' keyword we can get interface from derived class.
  • By using interfaces multiple inheritance is possible.

For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to [email protected]

Help Others, Please Share

What is class and interface in PHP?

A PHP interface defines a contract which a class must fulfill. If a PHP class is a blueprint for objects, an interface is a blueprint for classes. Any class implementing a given interface can be expected to have the same behavior in terms of what can be called, how it can be called, and what will be returned.

What is abstract class and interface class?

The Abstract class and Interface both are used to have abstraction. An abstract class contains an abstract keyword on the declaration whereas an Interface is a sketch that is used to implement a class. Explore more differences between abstract class and interface in java.

What is abstract class in PHP?

Abstract classes and methods are when the parent class has a named method, but need its child class[es] to fill out the tasks. An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code.

What is the class in PHP?

Class is a programmer-defined data type, which includes local methods and local variables. Class is a collection of objects. Object has properties and behavior.

Chủ Đề