svn: /phpdoc/ja/trunk/language/oop5/ properties.xml

[email protected] (Yoshinari Takaoka)
Newsgroups php.doc.ja
Message-ID <[email protected]>
mumumu                                   Mon, 08 Jun 2020 23:53:06 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=350016

Log:
more details for typed properties

Changed paths:
    U   phpdoc/ja/trunk/language/oop5/properties.xml
svn-diffs-350016.txt (text/x-diff, 8.3 KB)
Modified: phpdoc/ja/trunk/language/oop5/properties.xml
===================================================================
--- phpdoc/ja/trunk/language/oop5/properties.xml	2020-06-08 20:49:01 UTC (rev 350015)
+++ phpdoc/ja/trunk/language/oop5/properties.xml	2020-06-08 23:53:06 UTC (rev 350016)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!-- $Revision$ -->
-<!-- EN-Revision: 348562 Maintainer: takagi Status: ready -->
+<!-- EN-Revision: 350015 Maintainer: takagi Status: ready -->
 <!-- CREDITS: mumumu -->

  <sect1 xml:id="language.oop5.properties" xmlns="http://docbook.org/ns/docbook">
@@ -7,9 +7,9 @@
   <title>プロパティ</title>

   <para>
-   クラスのメンバ変数のことを「プロパティ」といいます。
-   それ以外に「属性」「フィールド」などという呼びかたを見たことがあるかもしれません。
-   しかし、このマニュアルでは「プロパティ」と呼ぶことにします。
+   クラスのメンバ変数のことを <emphasis>プロパティ</emphasis> といいます。
+   それ以外に <emphasis>属性</emphasis> や <emphasis>フィールド</emphasis> などという呼びかたを見たことがあるかもしれません。
+   しかし、このマニュアルでは <emphasis>プロパティ</emphasis> と呼ぶことにします。
    プロパティを定義するには <literal>public</literal>、<literal>protected</literal>
    あるいは <literal>private</literal>
    のいずれかのキーワードのあとに、オプションの型宣言を続け、
@@ -104,14 +104,16 @@
    </para>
   </note>

-  <para>
+  <sect2 xml:id="language.oop5.properties.heredoc-nowdoc">
+   <title>ヒアドキュメント と Nowdoc</title>
+   <para>
    PHP 5.3.0 以降、
    <link linkend="language.types.string.syntax.heredoc">ヒアドキュメント</link> や
-   <link linkend="language.types.string.syntax.nowdoc">nowdocs</link>
+   <link linkend="language.types.string.syntax.nowdoc">Nowdoc</link>
    は任意の静的データコンテキストで使えます。プロパティの宣言時にも使用可能です。
    <example>
-    <title>nowdoc を使ったプロパティの初期化</title>
-    <programlisting role="php">
+    <title>Nowdoc を使ったプロパティの初期化</title>
+     <programlisting role="php">
 <![CDATA[
 <?php
 class foo {
@@ -125,15 +127,18 @@
 }
 ?>
 ]]>
-    </programlisting>
-   </example>
-  </para>
-  <note>
-   <para>
-    Nowdoc とヒアドキュメントは PHP 5.3.0 以降で使用可能です。
+     </programlisting>
+    </example>
    </para>
-  </note>
+   <note>
+    <para>
+     Nowdoc とヒアドキュメントは PHP 5.3.0 以降で使用可能です。
+    </para>
+   </note>
+  </sect2>

+  <sect2 xml:id="language.oop5.properties.typed-properties">
+   <title>型宣言</title>
   <para>
    PHP 7.4.0 以降は、プロパティの定義に型宣言を含めることができます。
    但し、<literal>callable</literal> 型を除きます。
@@ -146,9 +151,9 @@
 class User
 {
     public int $id;
-    public string $name;
+    public ?string $name;

-    public function __construct(int $id, string $name)
+    public function __construct(int $id, ?string $name)
     {
         $this->id = $id;
         $this->name = $name;
@@ -155,24 +160,181 @@
     }
 }

-$user = new User(1234, "php");
-echo "ID: " . $user->id;
-echo "\n";
-echo "Name: " . $user->name;
+$user = new User(1234, null);

+var_dump($user->id);
+var_dump($user->name);
+
 ?>
 ]]>
-    </programlisting>
-    &example.outputs;
-    <screen>
+     </programlisting>
+     &example.outputs;
+     <screen>
 <![CDATA[
-ID: 1234
-Name: php
+int(1234)
+NULL
 ]]>
     </screen>
    </example>
   </para>

+  <para>
+    型付きプロパティは、アクセスする前に初期化しなければいけません。
+    初期化しないと、<classname>Error</classname> がスローされます。
+    <example>
+     <title>プロパティにアクセスする</title>
+     <programlisting role="php">
+<![CDATA[
+<?php
+
+class Shape
+{
+    public int $numberOfSides;
+    public string $name;
+
+    public function setNumberOfSides(int $numberOfSides): void
+    {
+        $this->numberOfSides = $numberOfSides;
+    }
+
+    public function setName(string $name): void
+    {
+        $this->name = $name;
+    }
+
+    public function getNumberOfSides(): int
+    {
+        return $this->numberOfSides;
+    }
+
+    public function getName(): string
+    {
+        return $this->name;
+    }
+}
+
+$triangle = new Shape();
+$triangle->setName("triangle");
+$triangle->setNumberofSides(3);
+var_dump($triangle->getName());
+var_dump($triangle->getNumberOfSides());
+
+$circle = new Shape();
+$circle->setName("circle");
+var_dump($circle->getName());
+var_dump($circle->getNumberOfSides());
+?>
+]]>
+     </programlisting>
+     &example.outputs;
+     <screen>
+<![CDATA[
+string(8) "triangle"
+int(3)
+string(6) "circle"
+
+Fatal error: Uncaught Error: Typed property Shape::$numberOfSides must not be accessed before initialization
+]]>
+     </screen>
+    </example>
+   </para>
+
+   <sect3 xml:id="language.oop5.properties.typed-properties.valid-types">
+    <title>有効なプロパティの型</title>
+    <informaltable>
+     <tgroup cols="3">
+      <thead>
+       <row>
+        <entry>型</entry>
+        <entry>説明</entry>
+        <entry>必要なPHPのバージョン</entry>
+       </row>
+      </thead>
+      <tbody>
+       <row>
+        <entry><type>bool</type></entry>
+        <entry>
+         プロパティの値は <type>boolean</type> 型でなければなりません。
+        </entry>
+        <entry>PHP 7.4.0</entry>
+       </row>
+       <row>
+        <entry><type>int</type></entry>
+        <entry>
+         プロパティの値は <type>integer</type> 型でなければなりません。
+        </entry>
+        <entry>PHP 7.4.0</entry>
+       </row>
+       <row>
+        <entry><type>float</type></entry>
+        <entry>
+         プロパティの値は <type>float</type> (浮動小数点型の数値) 型でなければなりません。
+        </entry>
+        <entry>PHP 7.4.0</entry>
+       </row>
+       <row>
+        <entry><type>string</type></entry>
+        <entry>
+         プロパティの値は <type>string</type> 型でなければなりません。
+        </entry>
+        <entry>PHP 7.4.0</entry>
+       </row>
+       <row>
+        <entry><type>array</type></entry>
+        <entry>
+         プロパティの値は <type>array</type> 型でなければなりません。
+        </entry>
+        <entry>PHP 7.4.0</entry>
+       </row>
+       <row>
+        <entry><literal>object</literal></entry>
+        <entry>
+         プロパティの値は <type>object</type> 型でなければなりません。
+        </entry>
+        <entry>PHP 7.4.0</entry>
+       </row>
+       <row>
+        <entry><literal>iterable</literal></entry>
+        <entry>
+         プロパティの値は、<type>array</type> 型か、
+         <interfacename>Traversable</interfacename> のインスタンスでなければなりません。
+        </entry>
+        <entry>PHP 7.4.0</entry>
+       </row>
+       <row>
+        <entry><literal>self</literal></entry>
+        <entry>
+         プロパティの値は、プロパティが定義されたクラスのインスタンスでなければなりません。
+        </entry>
+        <entry>PHP 7.4.0</entry>
+       </row>
+       <row>
+        <entry><literal>parent</literal></entry>
+        <entry>
+         プロパティの値は、プロパティが定義されたクラスの親クラスのインスタンスでなければなりません。
+        </entry>
+        <entry>PHP 7.4.0</entry>
+       </row>
+       <row>
+        <entry>Class/interface name</entry>
+        <entry>
+         プロパティの値は、指定したクラスまたはインターフェイスのインスタンスでなければなりません。
+        </entry>
+        <entry>PHP 7.4.0</entry>
+       </row>
+       <row>
+        <entry>?type</entry>
+        <entry>
+         プロパティの値は、指定された型か、 &null; でなければなりません。
+        </entry>
+        <entry>PHP 7.4.0</entry>
+       </row>
+      </tbody>
+     </tgroup>
+    </informaltable>
+   </sect3>
+  </sect2>
+
  </sect1>

 <!-- Keep this comment at the end of the file
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.