svn: /phpdoc/ja/trunk/language/oop5/ traits.xml
| From: | TAKAGI Masahiro | Date: | Wed, 23 Nov 2011 21:58:16 +0000 |
| Subject: | svn: /phpdoc/ja/trunk/language/oop5/ traits.xml | ||
| Groups: | php.doc.ja | ||
| Request: | Send a blank email to doc-ja+get-3155@lists.php.net to get a copy of this message | ||
takagi Wed, 23 Nov 2011 21:58:16 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=319728
Log:
sync with en.
Changed paths:
U phpdoc/ja/trunk/language/oop5/traits.xml
Modified: phpdoc/ja/trunk/language/oop5/traits.xml
===================================================================
--- phpdoc/ja/trunk/language/oop5/traits.xml 2011-11-23 21:24:34 UTC (rev 319727)
+++ phpdoc/ja/trunk/language/oop5/traits.xml 2011-11-23 21:58:16 UTC (rev 319728)
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
-<!-- EN-Revision: 317663 Maintainer: takagi Status: ready -->
+<!-- EN-Revision: 319725 Maintainer: takagi Status: ready -->
<sect1 xml:id="language.oop5.traits" xmlns="http://docbook.org/ns/docbook">
<title>トレイト</title>
<para>
@@ -390,21 +390,74 @@
<programlisting role="php">
<![CDATA[
<?php
- trait Singleton {
- public static function getInstance() { ... }
- }
+trait StaticExample {
+ public static function doSomething() {
+ return 'Doing something';
+ }
+}
- class MySingleton extends SomeUnrelatedSuperClass {
- use Singleton;
- }
+class Example {
+ use StaticExample;
+}
- MySingleton::getInstance();
+Example::doSomething();
?>
]]>
</programlisting>
</example>
</sect2>
+
+ <sect2 xml:id="language.oop5.traits.properties">
+ <title>プロパティ</title>
+ <para>
+ トレイトにはプロパティも定義できます。
+ </para>
+ <example xml:id="language.oop5.traits.properties.example">
+ <title>プロパティの定義</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+trait PropertiesTrait {
+ public $x = 1;
+}
+class PropertiesExample {
+ use PropertiesTrait;
+}
+
+$example = new PropertiesExample;
+$example->x;
+?>
+]]>
+ </programlisting>
+ </example>
+ <para>
+
トレイトでプロパティを定義したときは、クラスでは同じ名前のプロパティを定義できません。
+
定義しようとすると、エラーが発生します。クラス側での定義がトレイトでの定義と互換性がある
+ (可視性も初期値も同じ) 場合は <constant>E_STRICT</constant>、
+ それ以外の場合は fatal error となります。
+ </para>
+ <example xml:id="language.oop5.traits.properties.conflicts">
+ <title>衝突の解決</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+trait PropertiesTrait {
+ public $same = true;
+ public $different = false;
+}
+
+class PropertiesExample {
+ use PropertiesTrait;
+ public $same = true; // Strict Standards
+ public $different = true; // Fatal error
+}
+?>
+]]>
+ </programlisting>
+ </example>
+ </sect2>
+
</sect1>
<!-- Keep this comment at the end of the file