mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
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
This commit is contained in:
parent
bc8b580e62
commit
ac5ed968ff
1 changed files with 47 additions and 2 deletions
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue