svn: /phpdoc/en/trunk/language/oop5/ traits.xml

From: Date: Wed, 29 Jun 2011 06:40:04 +0000
Subject: svn: /phpdoc/en/trunk/language/oop5/ traits.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-8333@lists.php.net to get a copy of this message
frozenfire Wed, 29 Jun 2011 06:40:04 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=312619 Log: Started the OOP5 traits documentation. Changed paths: A phpdoc/en/trunk/language/oop5/traits.xml Added: phpdoc/en/trunk/language/oop5/traits.xml =================================================================== --- phpdoc/en/trunk/language/oop5/traits.xml (rev 0) +++ phpdoc/en/trunk/language/oop5/traits.xml 2011-06-29 06:40:04 UTC (rev 312619) @@ -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

« previous php.doc.cvs (#8333) next »