Difference between Implements and Extends?

I know how the extends pointer in PHP OOP works, but how does the implements one work…


class one
{
var $one;
}

class two implements one
{
var $two;
}

how does this work?

I know how the extends pointer in PHP OOP works

What does that mean?

deleted

Implements is for interfaces. An interface is where you define the skeleton structure of a class and then you implement the skeleton with another class that puts some meat on the bones!

For example, imagine we are creating a library that will allow a user to connect to a number of different databases using a standard API. What we will create is a number of different classes with this API. This is where an interface is used.

<?php

    interface iDatabaseConnection {

        public function connect ( $host, $username, $password );

    }  // End interface

?>

Now any class that implements this interface has to provide a connect method that takes the three parameters $host, $username and $password or PHP will throw an error.

<?php

    class MySQLConnection implements iDatabaseConnection {

        public function connect ( $host, $username, $password ) {

            // Connect here

        }  // End method

    }  // End class

?>

This way you can be sure that every class that implements iDatabaseConnection will stick to the exact same API structure

Sean :slight_smile:

So basically its a template

Yes, but don’t call it that :stuck_out_tongue:

Sean :slight_smile: