I am a little confused as to how you use interfaces and what they really are. the idea i get about them is as follows:

Multiple inheritance does not exist in php5 so an interface can be used to replicate this.

As i understand this means you have access to all public functions. But does this mean that you can use public functions in all classes you have?

yes i have read the manual and i still dont understand.

    Multiple inheritance does not exist in php5 so an interface can be used to replicate this.

    Not quite true, in my opinion. Multiple inheritance is more powerful than interfaces, but with the cost of additional complexity (e.g., diamond inheritance problem). I see it as "multiple inheritance can be used to simulate interfaces" instead. For example, one could simulate implementing multiple interfaces by deriving from multiple abstract base classes that have no member variables and only public member functions.

    As i understand this means you have access to all public functions. But does this mean that you can use public functions in all classes you have?

    You do not have access to anything. Rather, you have to implement the functions specified in the interface. Of course, since these functions are implemented by the class, the class can use them as per normal.

    When you access an object via an interface, then you are restricted to using only those functions specified by the interface, even though the object may be of a class that has other functions.

      The main use I've found in using interfaces in PHP is that they provide a means for a class to have multiple "types", so that it is a valid argument to any method parameter which uses any of those types as a "type hint"; which allows that method to know that the object passed via that argument is guaranteed to implement the methods specified in that interface.

      That being said, as of yet I have not gotten to the point in my OOP coding where I've found myself using interfaces much at all. I've gotten into using abstract classes quite a bit, but either interfaces have not been of much use to me yet, or else I have not really fully comprehended all their benefits so far. 🙂

        NogDog wrote:

        The main use I've found in using interfaces in PHP is that they provide a means for a class to have multiple "types", so that it is a valid argument to any method parameter which uses any of those types as a "type hint"; which allows that method to know that the object passed via that argument is guaranteed to implement the methods specified in that interface.

        Indeed! Interfaces allow classes to have common functionality. This functionality can always be broken by improper implementation, but it serves as a reminder to the programmer of the features expected.

          IMHO the interface is very powerful. Its a contract. The contract says objects implementing this interface promise to somehow implement all methods of the interface. If my code references the interface, and not the implementation, then I can swap out different implementations as long as they are the same interface (ie agree to same contract). That gives me a warm fuzzy feeling!

            Well thanks for the replies, really i was asking for a more basic explanation seeing as i have only started delving into PHP let alone OOP. If you can, perhaps explain to me with less technical jargon?!

              When you have a class "implement" an interface, then that class is required to provide an implementation of each and every method defined in that interface. An additional effect is that the implementing class is then considered to have that interface as one of its "types". As an example, if class A implements interface B, and if class C has a method which requires an object of type B, then (1) an object of class A can legally be supplied as an argument to that method and (2) that method can depend on any such object having implemented all the methods specified in interface B.

              interface B
              {
                 public function add($a, $b);
              }
              
              class A implements B
              {
                 // must implement all methods in B:
                 public function add($a, $b)
                 {
                    return($a + $b);
                 }
              }
              
              class C
              {
                 // require that $obj parameter be of type "B":
                 public function example(B $obj)
                 {
                    // we know that $obj must provide a method "add()":
                    echo $obj->add(3, 4);
                 }
              }
              
              $testA = new A();
              $testC = newC();
              $testC->example($testA); // echoes "7";
              
              // However...
              class X
              {
                 public function add($a, $b)
                 {
                    // whatever....
                 }
              }
              
              $testX = new X();
              $testC->example($testX); // throws an error, as $testX is not of type "B"
              
                Write a Reply...