mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Better example
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@168783 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
04f19022bc
commit
a4c15fe8d9
1 changed files with 40 additions and 19 deletions
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue