From ac5ed968ff8350fd736e38af04587180e1059a3d Mon Sep 17 00:00:00 2001 From: Sherif Ramadan <googleguy@php.net> Date: Tue, 9 Oct 2012 13:24:53 +0000 Subject: [PATCH] Updated abstract method signature information and added an example to clarify on optional arguments. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@327966 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/oop5/abstract.xml | 49 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/language/oop5/abstract.xml b/language/oop5/abstract.xml index 8c054f5ee2..1085d97f0c 100644 --- a/language/oop5/abstract.xml +++ b/language/oop5/abstract.xml @@ -19,8 +19,10 @@ if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. Furthermore the signatures of the methods must match, i.e. the type hints and the number - of required arguments must be the same. This also applies to constructors as - of PHP 5.4. Before 5.4 constructor signatures could differ. + of required arguments must be the same. For example, if the child class + defines an optional argument, where the abstract method's signature does + not, there is no conflict in the signature. This also applies to constructors + as of PHP 5.4. Before 5.4 constructor signatures could differ. </para> <example> @@ -83,6 +85,49 @@ FOO_ConcreteClass2 </screen> </example> + <example> + <title>Abstract class example</title> + <programlisting role="php"> +<![CDATA[ +<?php +abstract class AbstractClass +{ + // Our abstract method only needs to define the required arguments + abstract protected function prefixName($name); + +} + +class ConcreteClass extends AbstractClass +{ + + // Our child class may define optional arguments not in the parent's signature + public function prefixName($name, $separator = ".") { + if ($name == "Pacman") { + $prefix = "Mr"; + } elseif ($name == "Pacwoman") { + $prefix = "Mrs"; + } else { + $prefix = ""; + } + return "{$prefix}{$separator} {$name}"; + } +} + +$class = new ConcreteClass; +echo $class->prefixName("Pacman"), "\n"; +echo $class->prefixName("Pacwoman"), "\n"; +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +Mr. Pacman +Mrs. Pacwoman +]]> + </screen> + </example> + <para> Old code that has no user-defined classes or functions named 'abstract' should run without modifications.