What is the use of interface and abstract class in php?
IntroductionBefore 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. Show 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 ClassAs 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:
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 ClassIn 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:
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.
ConclusionNow that you have learned abstract class and interface differences, it will be much easier to implement them on your project. Also, just would like to add here that just because any other OO language has some kind of interfaces and abstraction too doesn't mean they have the same meaning and purpose as in PHP. The use of abstraction/interfaces is slightly different while interfaces in PHP actually don't have a real function. They merely are used for semantic and scheme-related reasons. The point is to have a project as much flexible as possible, expandable and safe for future extensions regardless whether the developer later on has a totally different plan of use or not. If your English is not native you might lookup what Abstraction and Interfaces actually are. And look for synonyms too. And this might help you as a metaphor: INTERFACE Let's say, you bake a new sort of cake with strawberries and you made up a recipe describing the ingredients and steps. Only you know why it's tasting so well and your guests like it. Then you decide to publish your recipe so other people can try that cake as well. The point here is
Exactly THIS is what describes interfaces. It is a guide, a set of instructions which observe the content of the recipe. Same as if you would create a project in PHP and you want to provide the code on GitHub or with your mates or whatever. An interface is what people can do and what you should not. Rules that hold it - if you disobey one, the entire construct will be broken. ABSTRACTION To continue with this metaphor here... imagine, you are the guest this time eating that cake. Then you are trying that cake using the recipe now. But you want to add new ingredients or change/skip the steps described in the recipe. So what comes next? Plan a different version of that cake. This time with black berries and not straw berries and more vanilla cream...yummy. This is what you could consider an extension of the original cake. You basically do an abstraction of it by creating a new recipe because it's a lil different. It has a few new steps and other ingredients. However, the black berry version has some parts you took over from the original - these are the base steps that every kind of that cake must have. Like ingredients just as milk - That is what every derived class has. Now you want to exchange ingredients and steps and these MUST be defined in the new version of that cake. These are abstract methods which have to be defined for the new cake, because there should be a fruit in the cake but which? So you take the black berries this time. Done. There you go, you have extended the cake, followed the interface and abstracted steps and ingredients from it. What is the use of abstract class and interface?The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.
What is the use of 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 the use of 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.
Where do we use abstract class and interface in real life scenario?A concrete example of an abstract class would be a class called Animal. You see many animals in real life, but there are only kinds of animals. That is, you never look at something purple and furry and say "that is an animal and there is no more specific way of defining it".
|