svn: /phpdoc/en/trunk/language/ oop5/traits.xml oop5.xml
| From: | Justin Martin | Date: | Thu, 21 Jul 2011 21:49:37 +0000 |
| Subject: | svn: /phpdoc/en/trunk/language/ oop5/traits.xml oop5.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-8524@lists.php.net to get a copy of this message | ||
frozenfire Thu, 21 Jul 2011 21:49:37 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=313564
Log:
Added traits to oop5 index.
Fixed some wording and added some in the traits document.
Added trait visibility modification information to the traits document.
Changed paths:
U phpdoc/en/trunk/language/oop5/traits.xml
U phpdoc/en/trunk/language/oop5.xml
Modified: phpdoc/en/trunk/language/oop5/traits.xml
===================================================================
--- phpdoc/en/trunk/language/oop5/traits.xml 2011-07-21 21:48:20 UTC (rev 313563)
+++ phpdoc/en/trunk/language/oop5/traits.xml 2011-07-21 21:49:37 UTC (rev 313564)
@@ -2,8 +2,10 @@
<!-- $Revision$ -->
<sect1 xml:id="language.oop5.traits" xmlns="http://docbook.org/ns/docbook">
<title>Traits</title>
-
<para>
+ As of PHP 5.4.0, PHP implements a method of code reuse called Traits.
+ </para>
+ <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
@@ -172,17 +174,32 @@
<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 two Traits insert a method with 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).
+ To resolve naming conflicts between Traits used in the same class,
+ the <literal>insteadof</literal> operator needs to be used to chose exactly
+ one of the conflicting methods.
</para>
+ <para>
+ Since this only allows one to exclude methods, the <literal>as</literal>
+ operator can be used to allow the inclusion of one of the conflicting
+ methods under another name.
+ </para>
<example xml:id="language.oop5.traits.conflict.ex1">
<title>Conflict Resolution</title>
+ <para>
+ In this example, Talker uses the traits A and B.
+ Since A and B have conflicting methods, it defines to use
+ the variant of smallTalk from trait B, and the variant of bigTalk from
+ trait A.
+ </para>
+ <para>
+ The Aliased_Talker makes use of the <literal>as</literal> operator
+ to be able to use B's bigTalk implementation under an additional alias
+ <literal>talk</literal>.
+ </para>
<programlisting role="php">
<![CDATA[
<?php
@@ -224,6 +241,36 @@
</example>
</sect2>
+ <sect2 xml:id="language.oop5.traits.visibility">
+ <title>Changing Method Visibility</title>
+ <para>
+ Using the <literal>as</literal> syntax, one can also adjust the visibility
+ of the method in the exhibiting class.
+ </para>
+ <example xml:id="language.oop5.traits.visibility.ex1">
+ <title>Changing Method Visibility</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+trait HelloWorld {
+ public function sayHello() {
+ echo 'Hello World!';
+ }
+}
+
+class MyClass1 {
+ use HelloWorld { sayHello as protected }
+}
+
+class MyClass2 {
+ use HelloWorld { doHelloWorld as private sayHello }
+}
+?>
+]]>
+ </programlisting>
+ </example>
+ </sect2>
+
<sect2 xml:id="language.oop5.traits.composition">
<title>Traits Composed from Traits</title>
<para>
Modified: phpdoc/en/trunk/language/oop5.xml
===================================================================
--- phpdoc/en/trunk/language/oop5.xml 2011-07-21 21:48:20 UTC (rev 313563)
+++ phpdoc/en/trunk/language/oop5.xml 2011-07-21 21:49:37 UTC (rev 313564)
@@ -40,6 +40,7 @@
&language.oop5.static;
&language.oop5.abstract;
&language.oop5.interfaces;
+ &language.oop5.traits;
&language.oop5.overloading;
&language.oop5.iterations;
&language.oop5.patterns;