Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Object-Oriented PHP Basics (Retired) Inheritance, Interfaces, and Exceptions Object Interfaces

I still do not see the point have interface if we can simple create our on objects anyways.

Object interfaces

4 Answers

This really vexed me when I was learning C#. One thing you'll hear a lot in relation to interfaces is the idea of a "code contract", and it's usually billed as a organizational tool for your program. An interface ensures that all methods which implement an interface will contain some particular method signatures. Failing to do so will result in a compile error, and in PHP's case, that means a useless web page. But what I always wondered was, why have a formal structure in programming for this? What good is this organizational tool if I still have to remember to implement the interface? It turns out, organization and readability are only part of the story. In the proud programming tradition of using car analogies, I offer the following:

I assume you know how to drive. If you don't, pretend that you do. Because you know how to drive, you can sit in the seat of any consumer vehicle and drive it without having to relearn anything. Even if you've been driving a Volkswagen for 20 years, you can sit behind a brand-new Mazda or Lexus and pilot it away. You can drive a car made in the 70's more or less the same way you can drive a car made yesterday. You can drive an SUV or a sporty coupe. How is this possible? Is it because every car is exactly the same, or are you just a really fast learner?

Well, cars are made with particular specifications in mind. Steering wheels, gas pedals, brake pedals, and turn signal levers are generally in the same place and generally work the same way. Cars with manual transmissions all have clutches and shifters in the same place and the shifters even (mostly) move in the same pattern. Although they may be radically different under the hood these cars implement an interface, and thus you are able to drive them all with relative ease.

in programming terms, you are an object with a property, 'car', that accepts any object that implements the 'consumer vehicle' interface. You know that you can call the accelerate(), brake(), signalLeft(), or shiftUp() methods even though you don't need to know how the car actually does these things. While programming, you may need one object to do go through a whole bunch of objects and have them do something. Interfaces allow you to write something very general, like

 foreach($objects as $object){
   if($object instanceOf iConsumerCar({
      $object->turnOnWipers();
    }
      //Other code here//
  }

It's not possible to tell you exactly when and why you would use this without more experience in writing object oriented code, but just keep in mind that you may need to reference objects as something more general than their class.

Yup!

gregsmith5 Hi, great answer but i wanted to know if i got it right. So its like making method such as forgotPwd(); cookieAlert(); logOut(); and in each there a logic for every action that is taken and called accordingly. So did i understand it right?

good analogy, and it makes sense. how about a practical analogy for the web?

Alena Holligan
Alena Holligan
Treehouse Teacher

The PHP-FIG's primary goal is to "find ways we can work better together." Interfaces are one of the things that make this possible, check out the PSR's (PHP Standard Recommendations) for more examples. Specifically the ones referring to "Interfaces"

http://www.php-fig.org/psr/

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

This was a great discussion at the PHP Users group in Portland. Interfaces are used as a was to specify the basic requirements so that a component can be switched out easily and also understood easily. You may even write one when you are bringing in code from another source. They are definitely not required, and sometimes not even the best idea. One of the biggest thing to think about when writing "clean" code is: how easy is this going to be for another developer, or even my future self, to understand :)

Thank you for the information.