What are Object interfaces?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

What are Object interfaces?

Post by Benjamin »

Can someone please explain to me what this does and why one would want to do it?

Code: Select all

// Declare the interface 'iTemplate'
interface iTemplate
{
   public function setVariable($name, $var);
   public function getHtml($template);
}

// Implement the interface
// This will work
class Template implements iTemplate
{
   private $vars = array();
 
   public function setVariable($name, $var)
   {
       $this->vars[$name] = $var;
   }
 
   public function getHtml($template)
   {
       foreach($this->vars as $name => $value) {
           $template = str_replace('{' . $name . '}', $value, $template);
       }

       return $template;
   }
}
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Interfaces are 'templates'

If a class implements an inteface, it must have the methods defined within the interface. Including the listed arguments. However the class is not limited to those methods only.

In other languages, such as Java, more 'rules' can be defined, such as what type the method must return etc.

Code: Select all

/*
* the interface..
*/
inteface iBar
{
    public function doSomething();
}

/*
* this class is OK - it contains the method doSomething()
* which is specified in the iBar interface
*/
class Bar implements iBar
{
    private $foobar;

    public function doSomething ()
    {
        echo 'Hello World!';
    }
}

/*
* this class is NOT OK, it does not contain the method doSomething()
*/
class Boo implements iBar
{
    public doSomethingElse ()
    {
        echo 'Goodbye World!';
    }
}

/*
* here we specify that the constructer must be passed a single argument, 
* that must comply with the interface iBar specifications.
*/

class Foo
{
    public function __construct (iBar $obj)
    {
        $obj->doSomething();
    }
}

/*
* does not fail - the class Bar adheres to interface iBar specifications
*/
$foobar = new Foo(new Bar);

/*
* fails - the (psuedo) class OtherClass does not implement the iBar interface
*/
$foobar2 = new Foo(new OtherClass);
It's a way of ensuring that the object/arguement being received contains the functions/methods that are required for the object to complete it's objective.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

They only really come in useful when you write something that other developers can write their own classes to hook into, since if they write their own class and it *needs* to work with your code it most likely requires a particular set of methods (interface/API).

Apart from that they provide a min-documentation or summary of a classes implemented methods but that's a bit of an overkill if the only reason you've defined the interface is to do that.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Is this a Factory? Or similar to a Factory?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

A factory is an entity that creates objects for you...

An interface is like a contract. Every class that implements the interface signs a contract with the compiler that it implements all the methods mentionned in the contract (interface).

The advantage of implementing an interface instead of inheriting from a base class is that you can't inherit from 2 classes.. But you can implement more than one interface...

The other advantage is that with inheritance you're not forcing the class into a specific class hierarchy (unlike inheritance).
Post Reply