What is use of interface class in php?
PHP - What are Interfaces?Interfaces allow you to specify what methods a class should implement. Show Interfaces make it easy to use a variety of different classes in the same way. When one or more classes use the same interface, it is referred to as "polymorphism". Interfaces are declared with the Syntax interface InterfaceName { PHP - Interfaces vs. Abstract ClassesInterface are similar to abstract classes. The difference between interfaces and abstract classes are:
PHP - Using InterfacesTo implement an interface, a class must use the A class that implements an interface must implement all of the interface's methods. Exampleinterface Animal { class
Cat implements Animal { $animal = new Cat(); Try it Yourself » From the example above, let's say that we would like to write software which manages a group of animals. There are actions that all of the animals can do, but each animal does it in its own way. Using interfaces, we can write some code which can work for all of the animals even if each animal behaves differently: Example// Interface definition // Class definitions class Dog implements Animal { class Mouse implements Animal { // Create a list of animals // Tell the animals to make a sound Try it Yourself » Example ExplainedCat, Dog and Mouse are all classes that implement the Animal interface, which means that all of them are able to make a sound using the Since the interface does not tell the classes how to implement the method, each animal can make a sound in its own way. Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are implemented. Interfaces share a namespace with classes and traits, so they may not use the same name. Interfaces are defined in the same way as a class, but with the All methods declared in an interface must be public; this is the nature of an interface. In practice, interfaces serve two complementary purposes:
Interfaces may define magic methods to require implementing classes to implement those methods.
|