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

Arif Zuhairi
Arif Zuhairi
1,637 Points

How to use the method if we don't define it?

In the video show that the definitions of function howToRide is in the class..so what's really mean of not having to define how these methods are handled?? Can anyone explain to me..if you can show some example of code..I'm really appreciated it

3 Answers

Jose Soto
Jose Soto
23,407 Points

An interface is a blueprint for a class. We tell it which methods every class that implements it must have.

For example, let's say we're going to make a blueprint (interface) for smartphones. What must every smartphone have?

  • a way to access the internet
  • a way to send a text message

The list can go on. However, each smartphone is different. You would use Safari on an iPhone to access the internet, but you would use Chrome on an Android. You would use Messages on an iPhone, but Hangouts on an Android. We can also think about Microsoft smartphones and smartphones running Firefox OS and Blackberry.

So how do we create an interface that will help us make classes for each of these smartphones? Easy! Just tell the interface that our class will need to have a way to access the internet. We don't need to know exactly how it will do that just yet. You can worry about that when you define the actual class.

Here is a rough example of what I am referring to:

<?php

// We just want to make a blueprint for all smartphones.
// Every class that implements this interface must use both of these methods.

interface smartphone
{
    public function sendText($text_message);
    public function openWebsite($url);
}


//When we define the class, THEN we actually have to worry about 
//how the method is going to work.


class iPhone implements smartphone
{
    public function sendText($text_message)
    {
        //Open Messages
        //Send Text
    }

    public function openWebsite($url)
    {
        //Open Safari
        //Go to URL
    }
}


class Microsoft implements smartphone
{
    public function sendText($text_message)
    {
        //Open Skype
        //Send Text
    }

    public function openWebsite($url)
    {
        //Open Internet Explorer
        //Go to URL
    }
}

class Android implements smartphone
{
    public function sendText($text_message)
    {
        //Open Hangouts
        //Send Text
    }

    public function openWebsite($url)
    {
        //Open Chrome
        //Go to URL
    }
}

Every class the implements the smartphone interface MUST use both of the methods within the interface. If you do not, then you will get an error message. You can't have a smartphone without a way to get on the internet!!

Hope that helps a bit. Cheers!

Man thanks... I didn't understand at video but i understood with your example.

Thank-you, I also understood the purpose of interfaces much more clearly after reading your comment!

Thanks !

Jaime Rios
Jaime Rios
Courses Plus Student 21,100 Points

Thanks so much. I find this example better than the one in the video.

Arif Zuhairi
Arif Zuhairi
1,637 Points

Oh great..that clear it up ; ) Now I understand clearly..Thanks Jose

Jonah Shi
Jonah Shi
10,140 Points

Thank you Jose, I was a bit confused after watching the video, but you explain interface concept very well to clear my confusion.