mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Added a bunch of examples from the RFC.
Fixed the file ending comment. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@312791 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
0fb8963bfb
commit
92e367295e
1 changed files with 258 additions and 1 deletions
|
@ -72,6 +72,263 @@ class ezcReflectionFunction extends ReflectionFunction {
|
|||
/* ... */
|
||||
}
|
||||
?>
|
||||
]]>
|
||||
</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>
|
||||
|
@ -96,4 +353,4 @@ 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
|
||||
vi: ts=1 sw=1
-->
|
||||
|
|
Loading…
Reference in a new issue