Why do we need Interfaces in PHP?The interface is another property of PHP that enables the developer to build a program without any complex methods. It inherits the same public method that a class holds. Due to these properties, an interface can define methods. Still, we cannot define the method in that particular interface, and we have to define the method inside the child class where we call the interface. Interfaces are also similar to classes, and the only difference is class is altered with an interface, and the methods do not have any content defined inside. We use interfaces in PHP. A single class cannot implement two functions with the same method name as the compiler will return an error. Similarly, like traits, we can also use an interface to access multiple inheritances as a class can initiate multiple interfaces, which will eventually help in inheriting traits from multiple parent classes Example Output: very sweet !!! In the above program, we have declared an interface fruit with a function taste() inside. We have declared a class mango and assigned the interface fruit to it; we have defined the function taste(). Finally, we have created an object all called our child class. Due to the interface, our child class mango can access the interface fruit and retrieve the function taste() from it Uses of Interface 1) To inherit properties of the parent class To implement an interface using class, we have to use the implement keyword and the expand keyword to assign the hierarchy of the parent class Syntax: Example: Output: this is the first class this is interface which is defined inside child class this is an inherited child class using an interface In the above program, we have a parent class parentClass which contains a function parentFunction(), and we have an interface called firstInterface, which holds the function named interfaceFunction(). Still, we cannot define the declared function inside our created interface, and we have to define the declared function inside the child class. In the end, we have a child class with the name childClass with function childFunction() and inherited the function from the interface using the extends and implement property of PHP. Also, we have defined our interfaceFunction() in the child. And to access the classes and interface, we have created an object obj and invoked all the functions using the same object. 2) To inherit properties of multiple interfaces To implement an interface using class, we have to use the implement keyword, and a single class can even implement more than one interface simultaneously Example: Output: this is the first interface this is the second interface this is a child inherited from both interfaces In the above program, we have declared two interfaces, first with the name firstInterface with function interfaceone() and second with name secondInterface with function onterfacetwo(). Still, we have only declared the interface functions and will define the same function in the child class. In the end, we have a child class with the name childClass with function childFunction() and inherited both the interfaces using the implements clause. Inside the function, we have given the definition of functions declared in interfaces. And to access the classes and interfaces, we have created an object with the name obj and called all the functions using the same object. Importance of using Interfaces:
Next Topicfile_put_contents() Function in PHP
|