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

From: Date: Sat, 02 Jul 2011 01:07:33 +0000
Subject: svn: /phpdoc/en/trunk/language/oop5/ traits.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-8392@lists.php.net to get a copy of this message
frozenfire Sat, 02 Jul 2011 01:07:33 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=312791 Log: Added a bunch of examples from the RFC. Fixed the file ending comment. Changed paths: U phpdoc/en/trunk/language/oop5/traits.xml Modified: phpdoc/en/trunk/language/oop5/traits.xml =================================================================== --- phpdoc/en/trunk/language/oop5/traits.xml 2011-07-02 01:02:49 UTC (rev 312790) +++ phpdoc/en/trunk/language/oop5/traits.xml 2011-07-02 01:07:33 UTC (rev 312791) @@ -75,6 +75,263 @@ ]]> </programlisting> </example> + <example xml:id="language.oop5.traits.examples.ex2"> + <title>Precedence Order Example</title> + <description>An inherited method from a base class is overridden by the + method inserted into MyHelloWorld from the SayWorld Trait. The behavior is + the same for methods defined in the MyHelloWorld class. The precedence order + is that methods from the current class override Trait methods, which in + turn override methods from the base class.</description> + <programlisting role="php"> +<![CDATA[ +<?php +class Base { + public function sayHello() { + echo 'Hello '; + } +} + +trait SayWorld { + public function sayHello() { + parent::sayHello(); + echo 'World!'; + } +} + +class MyHelloWorld extends Base { + use SayWorld; +} + +$o = new MyHelloWorld(); +$o->sayHello(); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +Hello World! +]]> + </screen> + </example> + <example xml:id="language.oop5.traits.examples.ex3"> + <title>Precedence Order Example #2</title> + <programlisting role="php"> +<![CDATA[ +<?php +trait HelloWorld { + public function sayHello() { + echo 'Hello World!'; + } +} + +class TheWorldIsNotEnough { + use HelloWorld; + public function sayHello() { + echo 'Hello Universe!'; + } +} + +$o = new TheWorldIsNotEnough(); +$o->sayHello(); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +Hello Universe! +]]> + </screen> + </example> + <example xml:id="language.oop5.traits.examples.ex4"> + <title>Multiple Traits Usage</title> + <programlisting role="php"> +<![CDATA[ +<?php +trait Hello { + public function sayHello() { + echo 'Hello '; + } +} + +trait World { + public function sayWorld() { + echo ' World'; + } +} + +class MyHelloWorld { + use Hello, World; + public function sayExclamationMark() { + echo '!'; + } +} + +$o = new MyHelloWorld(); +$o->sayHello(); +$o->sayWorld(); +$o->sayExclamationMark();> +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +Hello World! +]]> + </screen> + </example> + <example xml:id="language.oop5.traits.examples.ex5"> + <title>Conflict Resolution</title> + <programlisting role="php"> +<![CDATA[ +<?php +trait A { + public function smallTalk() { + echo 'a'; + } + public function bigTalk() { + echo 'A'; + } +} + +trait B { + public function smallTalk() { + echo 'b'; + } + public function bigTalk() { + echo 'B'; + } +} + +class Talker { + use A, B { + B::smallTalk insteadof A; + A::bigTalk insteadof B; + } +} + +class Aliased_Talker { + use A, B { + B::smallTalk insteadof A; + A::bigTalk insteadof B; + B::bigTalk as talk; + } +} +?> +]]> + </programlisting> + </example> + <example xml:id="language.oop5.traits.examples.ex6"> + <title>Traits Composed from Traits</title> + <programlisting role="php"> +<![CDATA[ +<?php +trait Hello { + public function sayHello() { + echo 'Hello '; + } +} + +trait World { + public function sayWorld() { + echo 'World!'; + } +} + +trait HelloWorld { + use Hello, World; +} + +class MyHelloWorld { + use HelloWorld; +} + +$o = new MyHelloWorld(); +$o->sayHello(); +$o->sayWorld(); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +Hello World! +]]> + </screen> + </example> + <example xml:id="language.oop5.traits.examples.ex7"> + <title>Express Requirements by Abstract Methods</title> + <programlisting role="php"> +<![CDATA[ +<?php +trait Hello { + public function sayHelloWorld() { + echo 'Hello'.$this->getWorld(); + } + abstract public function getWorld(); +} + +class MyHelloWorld { + private $world; + use Hello; + public function getWorld() { + return $this->world; + } + public function setWorld($val) { + $this->world = $val; + } +} +?> +]]> + </programlisting> + </example> + <example xml:id="language.oop5.traits.examples.ex8"> + <title>Static Variables</title> + <programlisting role="php"> +<![CDATA[ +<?php + trait Counter { + public function inc() { + static $c = 0; + $c = $c + 1; + echo "$c\n"; + } + } + + class C1 { + use Counter; + } + + class C2 { + use Counter; + } + + $o = new C1(); $o->inc(); // echo 1 + $p = new C2(); $p->inc(); // echo 1 +?> +]]> + </programlisting> + </example> + <example xml:id="language.oop5.traits.examples.ex9"> + <title>Static Methods</title> + <programlisting role="php"> +<![CDATA[ +<?php + trait Singleton { + public static function getInstance() { ... } + } + + class MySingleton extends SomeUnrelatedSuperClass { + use Singleton; + } + + MySingleton::getInstance(); +?> +]]> + </programlisting> + </example> </sect2> </sect1> @@ -96,4 +353,5 @@ End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml -vi: ts=1 sw=1 +vi: ts=1 sw=1 +-->

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