search for in the  
<Object AbstractionOverloading>
Last updated: Thu, 19 May 2005

Object Interfaces

Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.

Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined. Classes which implement an interface should do so using the implements keyword, and must have definitions for all the methods listed in the interface. Classes may implement more than one interface if desired by listing each interface split by a comma.

All methods declared in an interface must be public, this is the nature of an interface.

Stating that a class implements an interface, and then not implementing all the methods in the interface will result in a fatal error telling you which methods have not been implemented.

Example 19-17. Interface example

<?php
// 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;
   }
}

// This will not work
// Fatal error: Class BadTemplate contains 1 abstract methods
// and must therefore be declared abstract (iTemplate::getHtml)
class BadTemplate implements iTemplate
{
   private
$vars = array();
 
   public function
setVariable($name, $var)
   {
      
$this->vars[$name] = $var;
   }
}

?>


User Contributed Notes
Object Interfaces
tobias_demuth at web dot de
04-May-2005 04:21
The statement, that you have to implement _all_ methods of an interface has not to be taken that seriously, at least if you declare an abstract class and want to force the inheriting subclasses to implement the interface.
Just leave out all methods that should be implemented by the subclasses. But never write something like this:

<?php

interface Foo {

     function
bar();

}

abstract class
FooBar implements Foo {

       abstract function
bar(); // just for making clear, that this
                                 // method has to be implemented

}

?>

This will end up with the following error-message:

Fatal error: Can't inherit abstract function Foo::bar() (previously declared abstract in FooBar) in path/to/file on line anylinenumber
Sebastian Mendel
11-Mar-2005 07:12
erik dot zoltan at msn dot com
25-Feb-2005 12:43
When should you use interfaces?  What are they good for?
Here are two examples. 

1. Interfaces are an excellent way to implement reusability. 
You can create a general interface for a number of situations
(such as a save to/load from disk interface.)  You can then
implement the interface in a variety of different ways (e.g. for
formats such as tab delimited ASCII, XML and a database.) 
You can write code that asks the object to "save itself to
disk" without having to worry what that means for the object
in question.  One object might save itself to the database,
another to an XML and you can change this behavior over
time without having to rewrite the calling code. 

This allows you to write reusable calling code that can work
for any number of different objects -- you don't need to know
what kind of object it is, as long as it obeys the common
interface. 

2. Interfaces can also promote gradual evolution.  On a
recent project I had some very complicated work to do and I
didn't know how to implement it.  I could think of a "basic"
implementation but I knew I would have to change it later. 
So I created interfaces in each of these cases, and created
at least one "basic" implementation of the interface that
was "good enough for now" even though I knew it would have
to change later. 

When I came back to make the changes, I was able to create
some new implementations of these interfaces that added the
extra features I needed.  Some of my classes still used
the "basic" implementations, but others needed the
specialized ones.  I was able to add the new features to the
objects themselves without rewriting the calling code in most
cases.  It was easy to evolve my code in this way because
the changes were mostly isolated -- they didn't spread all
over the place like you might expect.
mat.wilmots (at) wanadoo (dot) fr
20-Jan-2005 08:22
interfaces support multiple inheritance

<?php

interface SQL_Result extends SeekableIterator, Countable
{
  
// new stuff
}

abstract class
SQL_Result_Common
{
  
// just because that's what one would do in reality, generic implementation
}

class
SQL_Result_mysql extends SQL_Result_Common implements SQL_Result
{
  
// actual implementation
}

?>

This code raises a fatal error because SQL_Result_mysql doesn't implement the abstract methods of SeekableIterator (6) + Countable (1)
m.kurzyna AT nius waw pl
23-Dec-2004 08:04
You can have a class implementing two or more interfaces, just seperate them with a coma:

<?php

interface i_one {
public
one();
}

interface
i_two {
public
two();
}

class
oneANDtwo implements i_one, i_two {
public
one() {}
public
two() {}
}

?>

Of course all implemented interfaces will be tested, so one needs to have one() and two() in their class to avoid errors.
russ dot collier at gmail dot com
28-Nov-2004 12:24
You can also specify class constants in interfaces as well (similar to specifying 'public static final' fields in Java interfaces):

<?php

interface FooBar
{

   const
SOME_CONSTANT = 'I am an interface constant';

   public function
doStuff();

}

?>

Then you can access the constant by referring to the interface name, or an implementing class, (again similar to Java) e.g.:

<?php

class Baz implements FooBar
{

  
//....

}

print
Baz::SOME_CONSTANT;
print
FooBar::SOME_CONSTANT;

?>

Both of the last print statements will output the same thing: the value of FooBar::SOME_CONSTANT

<Object AbstractionOverloading>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT