svn: /phpdoc/ja/trunk/ appendices/tokens.xml language/oop5/basic.xml

[email protected] (Yoshinari Takaoka)
Newsgroups php.doc.ja
Message-ID <[email protected]>
mumumu                                   Sun, 29 Nov 2020 00:52:24 +0000

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

Log:
Document nullsafe operator

Closes GH-244.

Changed paths:
    U   phpdoc/ja/trunk/appendices/tokens.xml
    U   phpdoc/ja/trunk/language/oop5/basic.xml

Modified: phpdoc/ja/trunk/appendices/tokens.xml
===================================================================
--- phpdoc/ja/trunk/appendices/tokens.xml	2020-11-29 00:39:12 UTC (rev 351740)
+++ phpdoc/ja/trunk/appendices/tokens.xml	2020-11-29 00:52:24 UTC (rev 351741)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!-- $Revision$ -->
-<!-- EN-Revision: 351112 Maintainer: hirokawa Status: ready -->
+<!-- EN-Revision: 351735 Maintainer: hirokawa Status: ready -->
 <!-- CREDITS: takagi,mumumu -->

 <appendix xml:id="tokens" xmlns="http://docbook.org/ns/docbook">
@@ -589,6 +589,11 @@
      <entry><link linkend="language.oop5">クラスとオブジェクト</link></entry>
     </row>
     <row>
+     <entry><constant>T_NULLSAFE_OBJECT_OPERATOR</constant></entry>
+     <entry>?-&gt;</entry>
+     <entry><link linkend="language.oop5">クラスとオブジェクト</link></entry>
+    </row>
+    <row>
      <entry><constant>T_OPEN_TAG</constant></entry>
      <entry>&lt;?php, &lt;? or &lt;%</entry>
      <entry><link linkend="language.basic-syntax.phpmode">HTML

Modified: phpdoc/ja/trunk/language/oop5/basic.xml
===================================================================
--- phpdoc/ja/trunk/language/oop5/basic.xml	2020-11-29 00:39:12 UTC (rev 351740)
+++ phpdoc/ja/trunk/language/oop5/basic.xml	2020-11-29 00:52:24 UTC (rev 351741)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!-- $Revision$ -->
-<!-- EN-Revision: 351711 Maintainer: takagi Status: ready -->
+<!-- EN-Revision: 351735 Maintainer: takagi Status: ready -->
 <!-- CREDITS: hirokawa,mumumu -->

 <sect1 xml:id="language.oop5.basic" xmlns="http://docbook.org/ns/docbook">
@@ -496,8 +496,58 @@
     </screen>
    </example>
   </sect2>
- </sect1>
+ <sect2 xml:id="language.oop5.basic.nullsafe">
+  <title>nullsafe メソッドとプロパティ</title>
+  <para>
+   PHP 8.0.0 以降では、プロパティやメソッドは
+   "nullsafe" 演算子 <literal>?-></literal> を使ってアクセスすることもできます。
+   nullsafe 演算子は既に述べたメソッドやプロパティと同じように振る舞いますが、
+   オブジェクトが &null; と評価された場合に、例外はスローされず、
+   &null; が返される点だけが異なります。
+   オブジェクトの評価がチェインの一部だった場合は、
+   残りのチェインはスキップされます。
+  </para>
+  <para>
+   この演算子の働きは、
+   オブジェクトにアクセスするたびに <function>is_null</function> でラップするコードに似ています。
+   しかし、よりコンパクトです。
+  </para>
+  <para>
+   <example>
+    <title>nullsafe 演算子</title>
+    <programlisting role="php">
+<![CDATA[
+<?php

+// PHP 8.0.0 以降は、このように書けます:
+$result = $repository?->getUser(5)?->name;
+
+// これは、以下のコードチェックと同等です:
+if (is_null($repository)) {
+    $result = null;
+} else {
+    $user = $repository->getUser(5);
+    if (is_null($user)) {
+        $result = null;
+    } else {
+        $result = $user->name;
+    }
+}
+?>
+]]>
+    </programlisting>
+   </example>
+  </para>
+  <note>
+   <para>
+    nullsafe 演算子は、プロパティやメソッドが値を返す際、
+    null が正しく、期待されうる値とみなせる場合に一番よく使います。
+    エラーを示すためなら、例外を投げるほうが好ましいです。
+   </para>
+  </note>
+ </sect2>
+</sect1>
+
 <!-- Keep this comment at the end of the file
 Local variables:
 mode: sgml
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.