Better example

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@168783 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Aidan Lister 2004-09-19 03:49:07 +00:00
parent 04f19022bc
commit a4c15fe8d9

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.3 $ -->
<!-- $Revision: 1.4 $ -->
<sect1 id="language.oop5.interfaces">
<title>Object Interfaces</title>
<para>
@ -15,6 +15,10 @@
in the interface. Classes may implement more than one interface if desired
by listing each interface split by a comma.
</para>
<para>
All methods declared in an interface must be public, this is the nature of an
interface.
</para>
<para>
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
@ -25,31 +29,48 @@
<programlisting role="php">
<![CDATA[
<?php
interface ITemplate
// Declare the interface 'iTemplate'
interface iTemplate
{
public function setVariable($name, $var);
public function getHtml($template);
public function setVariable($name, $var);
public function getHtml($template);
}
class Template implements ITemplate
// Implement the interface
// This will work
class Template implements iTemplate
{
private $vars = array();
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);
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;
}
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;
}
}
?>
]]>
</programlisting>
</example>