PHP class implements example
What is an interface?
An interface is a programming block which contains abstract methods, and used the same like abstract classes but with some differences. Interfaces can also be extended to the child classes but with “implements” keyword. Let’s see “PHP class implements” example for an Interface.
Like an abstract class, an interface also used to provide an abstraction layer in your program.
If you search, the dictionary meaning of an interface it is a point to communicate or a connection.
An interface will provide you a point to communicate with the class using its methods and hiding the underlying functionality of a method.
Confuse?
Let’s see an example:
What is a mouse of your computer? What is the screen? What is a keyboard?
All the above devices are user “interfaces” of a computer used to communicate with a computer without disclosing underlying hardware or software.
They provide a medium to communicate with the computer without us knowing the inner complexity.
The same kind of functionality will be provided by an interface in object oriented programming.
Some important features of interfaces:
- An interface contains abstract methods only. But methods will not have an “abstract” keyword.
- An interface doesn’t have any properties/variables defined in it but can have constants.
- All the methods in an interface should be declared public.
How to declare an interface?
<?php interface nameOfInterface { //Abstract methods without using abstract keyword public function method1(); public function method2(); } ?>
As shown above, the interface can be declared using “interface” keyword following the name of an interface.
In an interface, we can only declare methods but cannot implement them inside it, just like an abstract class.
The methods need not use an “abstract” keyword before them.
Methods declared inside an interface are by default abstract. And methods should be declared public only.
Private methods are not allowed in an interface.
An interface is not allowed to declare properties in it.
“implements” not “extends”
Like an abstract class, an interface can also be extended by the child classes but using an “implements” keyword and not “extends” keyword. PHP class implements an interface not extends.
<?php interface Animal { //Abstract methods without using abstract keyword public function walk(); public function talk(); } class Dog implements Animal { } ?>
In the above example, we have derived a child class “Dog” from an interface “Animal”.
Here we can say class “Dog” has implemented an interface “Animal”.
Run the above program.
You will get a Fatal error as “Fatal error: Class Dog contains 2 abstract methods and must, therefore, be declared abstract or implement the remaining methods”.
Above error occurs because we have not implemented or defined the functionalities of the abstract methods “walk” and “talk” inside a child class “Dog”.
This is because an interface enforces the child class to implement all the abstract methods of an interface.
Let’s implement the methods declared in an interface Animal into the Dog class.
<?php interface Animal { //Abstract methods without using abstract keyword public function walk(); public function talk(); } class Dog implements Animal { public function walk() { echo "Dog can walk on four legs"; } public function talk() { echo "<br>Dogs cannot talk like humans"; } } $dog = new Dog(); $dog->walk(); $dog->talk(); ?>
Multiple inheritances using interface
Unlike classes, you can inherit multiple interfaces using “implements” keyword and separate interface names with a comma.
Let’s see an example. PHP class implements example:
<?php interface Animal { //Abstract methods without using abstract keyword public function walk(); public function talk(); } interface Pets { //Abstract methods without using abstract keyword public function play(); public function protect(); } class Dog implements Animal, Pets { public function walk() { echo "Dog can walk on four legs"; } public function talk() { echo "<br>Dogs cannot talk like humans"; } public function play() { echo "<br>Dogs can play with humans"; } public function protect() { echo "<br>Dogs can protect humans"; } } $dog = new Dog(); $dog->walk(); $dog->talk(); $dog->play(); $dog->protect(); ?>
In the above example, I have created a new interface named “Pets” with two abstract methods “play” and “protect”.
The Pets interface is also implemented into the child class Dog along with Animal interface using a comma.
In this way, you can inherit as many interfaces as you want in your child class.
And force child class to have those methods in them implemented.
Interface inheriting interface
An interface can also inherit another interface using “extends” keyword.
<?php interface Animal { public function talk(); } interface Pets extends Animal { public function walk(); } class Dog implements Pets { public function walk() { echo "Dogs can walk"; } public function talk() { echo "<br>Dogs cannot talk"; } } $dog = new Dog(); $dog->walk(); $dog->talk(); ?>
We have extended an interface Animal into another interface Pets.
When you implement Pets interface to child class Dog, then in this case also you need to define the methods present in both interfaces i.e. walk and talk methods. Else you will get a fatal error.
Let’s revisit the important points in an interface
- An interface can only contain constants and abstract methods but not method definition and variable or properties.
- You cannot create an object of an interface. But you can collect the object of a class which implements an interface into the interface variable. We will see this thing in details in polymorphism and dependency injection.
- All the methods should be declared public inside an interface.
- Child classes can inherit interface using “implements” keyword.
- You can implement more than one interface in the child class.
In this tutorial, we have seen what is interface and how PHP class implements an interface in detail with example.
In the next tutorial, we will see another most important concept in Object Oriented PHP which Dependency Injection, in detail.
Stay tuned.