mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Started the OOP5 traits documentation.
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@312619 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
0d72504ba9
commit
1949cc2d35
1 changed files with 99 additions and 0 deletions
99
language/oop5/traits.xml
Normal file
99
language/oop5/traits.xml
Normal file
|
@ -0,0 +1,99 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- $Revision: 298604 $ -->
|
||||
<sect1 xml:id="language.oop5.traits" xmlns="http://docbook.org/ns/docbook">
|
||||
<title>Traits</title>
|
||||
|
||||
<para>
|
||||
Traits is a mechanism for code reuse in single inheritance languages such as
|
||||
PHP. A Trait is intended to reduce some limitations of single inheritance by
|
||||
enabling a developer to reuse sets of methods freely in several independent
|
||||
classes living in different class hierarchies. The semantics of the combination
|
||||
of Traits and classes is defined in a way which reduces complexity, and avoids
|
||||
the typical problems associated with multiple inheritance and Mixins.
|
||||
</para>
|
||||
<para>
|
||||
A Trait is similar to a class, but only intended to group functionality in a
|
||||
fine-grained and consistent way. It is not possible to instantiate a Trait on
|
||||
its own. It is an addition to traditional inheritance and enables horizontal
|
||||
composition of behavior; that is, the application of class members without
|
||||
requiring inheritance.
|
||||
</para>
|
||||
|
||||
<sect2 xml:id="language.oop5.traits.precedence">
|
||||
<title>Precedence</title>
|
||||
<para>
|
||||
An inherited member from a base class is overridden by a member inserted
|
||||
by a Trait. The precedence order is that members from the current class
|
||||
override Trait methods, which in return override inherited methods.
|
||||
</para>
|
||||
</sect2>
|
||||
|
||||
<sect2 xml:id="language.oop5.traits.multiple">
|
||||
<title>Multiple Traits</title>
|
||||
<para>
|
||||
Multiple Traits can be inserted into a class by listing them in the use
|
||||
statement, separated by commas.
|
||||
</para>
|
||||
</sect2>
|
||||
|
||||
<sect2 xml:id="language.oop5.traits.conflict">
|
||||
<title>Conflict Resolution</title>
|
||||
<para>
|
||||
If two Traits insert a member by the same name, a fatal error is produced,
|
||||
if the conflict is not explicitly resolved.
|
||||
</para>
|
||||
<para>
|
||||
To resolve conflicted naming among Traits, one can use the <literal>insteadof</literal>
|
||||
operator to explicitly define the precedence between said Traits. Or,
|
||||
one can use the <literal>as</literal> operator to alias the conflicting
|
||||
member name(s).
|
||||
</para>
|
||||
</sect2>
|
||||
|
||||
<sect2 xml:id="language.oop5.traits.examples">
|
||||
&reftitle.examples;
|
||||
<example xml:id="language.oop5.traits.examples.ex1">
|
||||
<title>Trait example</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
trait ezcReflectionReturnInfo {
|
||||
function getReturnType() { /*1*/ }
|
||||
function getReturnDescription() { /*2*/ }
|
||||
}
|
||||
|
||||
class ezcReflectionMethod extends ReflectionMethod {
|
||||
use ezcReflectionReturnInfo;
|
||||
/* ... */
|
||||
}
|
||||
|
||||
class ezcReflectionFunction extends ReflectionFunction {
|
||||
use ezcReflectionReturnInfo;
|
||||
/* ... */
|
||||
}
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
</sect2>
|
||||
</sect1>
|
||||
|
||||
<!-- Keep this comment at the end of the file
|
||||
Local variables:
|
||||
mode: sgml
|
||||
sgml-omittag:t
|
||||
sgml-shorttag:t
|
||||
sgml-minimize-attributes:nil
|
||||
sgml-always-quote-attributes:t
|
||||
sgml-indent-step:1
|
||||
sgml-indent-data:t
|
||||
indent-tabs-mode:nil
|
||||
sgml-parent-document:nil
|
||||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||||
sgml-exposed-tags:nil
|
||||
sgml-local-catalogs:nil
|
||||
sgml-local-ecat-files:nil
|
||||
End:
|
||||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||||
vim: et tw=78 syn=sgml
|
||||
vi: ts=1 sw=1
|
Loading…
Reference in a new issue