Page 1 of 1
What are Object interfaces?
Posted: Wed Jul 19, 2006 5:21 am
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;
}
}
Posted: Wed Jul 19, 2006 5:35 am
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.
Posted: Wed Jul 19, 2006 7:49 am
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.
Posted: Mon Jul 24, 2006 11:10 pm
by Benjamin
Is this a Factory? Or similar to a Factory?
Posted: Tue Jul 25, 2006 12:43 am
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).