Interface in PHP OOP – onlinecode

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.

Note :  While defining method in your interface, do not use abstract keyword in your methods.

                

  1. interface a
  2. {
  3. public functionabc();
  4. }
  5. interface b extends a
  6. {
  7. public functionxyz(Xyz $xyz);
  8. }
  9. // This will work
  10. class c implements b
  11. {
  12. public functionabc()
  13. {
  14. }
  15. public functionxyz(Xyz $xyz)
  16. {
  17. }
  18. }
  19. // This will not work and result in a fatal error
  20. class d implements b
  21. {
  22. public functionabc()
  23. {
  24. }
  25. public functionxyz(Abc $abc)
  26. {
  27. }
  28. }
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)   {  }}

Label :

PHP

OOP

Object Oriented Programming

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

For More Info See :: laravel And github

Leave a Comment

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

39  +    =  47

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US