Hasan Setiawan

Write, write, write give your wings on code!

Follow me on GitHub

Interface in PHP

What is Interface?

Interface is a collective function with only contain the function name.
There no behavior in the function declaration, just only function name.
All the function must be declared as a public. You use `implements` keyword to inherit
interface into your class. And PHP allowing you to inherit multiple interface.
All method in the interface must be implemented at inherited class.
If you skip even just one, you'll got an error.

              
                interface A {
                  public function do1();
                  public function do2();
                }

                interface B {
                  public function do5();
                  public function do6();
                }

                class C implements A, B { // you could implement multiple interface
                  public function do6() {
                    // you could define your custom behavior here
                  }

                  public function do1() {
                    // you could define your custom behavior here
                  }

                  public function do2() {
                    // you could define your custom behavior here
                  }

                  public function do5() {
                    // you could define your custom behavior here
                  }
                }