aidan Sat Sep 18 23:49:07 2004 EDT
Modified files:
/phpdoc/en/language/oop5 interfaces.xml
Log:
Better example
http://cvs.php.net/diff.php/phpdoc/en/language/oop5/interfaces.xml?r1=1.3&r2=1.4&ty=u
Index: phpdoc/en/language/oop5/interfaces.xml
diff -u phpdoc/en/language/oop5/interfaces.xml:1.3
phpdoc/en/language/oop5/interfaces.xml:1.4
--- phpdoc/en/language/oop5/interfaces.xml:1.3 Sun Sep 5 12:16:39 2004
+++ phpdoc/en/language/oop5/interfaces.xml Sat Sep 18 23:49:07 2004
@@ -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>
@@ -16,6 +16,10 @@
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
methods have not been implemented.
@@ -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 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 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>