Learn PHP interface in OOPs

PHP Interface OOPs can be used to access the properties of another class.

1.An interface is similar to a class except that it cannot contain code.
2.Interfaces cannot have properties
3.An interface can define method names and arguments, but not the contents of the methods.
4.Any classes implementing an interface must implement all methods defined by the interface.
5.A class can implement multiple interfaces.
6.An interface is declared using the "interface" keyword.
7.All interface methods must be public.
8.All methods in an interface are abstract, so they cannot be implemented in code and the abstract keyword is not necessary.
9.Classes can implement an interface while inheriting from another class at the same time.
10.Interfaces can't maintain Non-abstract methods.

How to declare and implement an interface?
We declare an interface with the interface keyword and, the class that inherits from an interface with the implements keyword. Let's see PHP Tutorial example:

interface boat {
function sink();
function scuttle();
function dock();

interface plane extends boat {
function takeoff();
function land();
function bailout();

class boatplane implements plane {
public function sink() { }
public function scuttle() { }
public function dock() { }
public function takeoff() { }
public function land() { }
public function bailout() { }

$obj = new boatplane();

Interfaces, like abstract classes, include abstract methods and constants. However, unlike abstract classes, interfaces can have only public methods, and cannot have variables.


Leave a Reply

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