Interface in PHP OOP – onlinecode
In this post we will give you information about Interface in PHP OOP – onlinecode. Hear we will give you detail about Interface in PHP OOP – onlinecodeAnd how to use it also give you demo for it if it is necessary.
Interface in PHP
An Interface does not contain any functionality, it only specify set of methods which classes must implement.
You can create interface in php oop by using interface keyword.
An Interface is like a template similar to abstract class but there are little bit differences where you can use only abstract methods
You can implement your interface in classes by using implements keyword.
You will have to notice that when you are going to implement interface in your class then method should be define with public accessibility.
If you will use other than public visibility then it will generate error.
When should you use interfaces? What are they good for?
Interface are a good way to implement reusability. you can define interface for a number of condition, you can then implement your interface in different ways.
Problem of multiple inheritance can be resolved by using interface.
- interface a
- {
- public functionabc();
- }
- interface b extends a
- {
- public functionxyz(Xyz $xyz);
- }
- // This will work
- class c implements b
- {
- public functionabc()
- {
- }
- public functionxyz(Xyz $xyz)
- {
- }
- }
- // This will not work and result in a fatal error
- class d implements b
- {
- public functionabc()
- {
- }
- public functionxyz(Abc $abc)
- {
- }
- }
interface a{ public function abc();}interface b extends a{ public function xyz(Xyz $xyz); }// This will workclass c implements b{ public function abc() { } public function xyz(Xyz $xyz) { }}// This will not work and result in a fatal errorclass d implements b{ public function abc() { } public function xyz(Abc $abc) { }}
Hope this code and post will helped you for implement Interface in PHP OOP – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs