[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-doc-cvs
Subject:    [DOC-CVS] =?utf-8?q?svn:_/phpdoc/en/trunk/_appendices/tokens.xml_language/oop5/basic.xml?=
From:       Christoph_Michael_Becker <cmb () php ! net>
Date:       2020-11-28 23:09:30
Message-ID: svn-cmb-1606604970-351735-1326260397 () svn ! php ! net
[Download RAW message or body]

cmb                                      Sat, 28 Nov 2020 23:09:30 +0000

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

Log:
Document nullsafe operator

Closes GH-244.

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

Modified: phpdoc/en/trunk/appendices/tokens.xml
===================================================================
--- phpdoc/en/trunk/appendices/tokens.xml	2020-11-28 22:49:17 UTC (rev 351734)
+++ phpdoc/en/trunk/appendices/tokens.xml	2020-11-28 23:09:30 UTC (rev 351735)
@@ -588,6 +588,11 @@
      <entry><link linkend="language.oop5">classes and objects</link></entry>
     </row>
     <row>
+     <entry><constant>T_NULLSAFE_OBJECT_OPERATOR</constant></entry>
+     <entry>?-&gt;</entry>
+     <entry><link linkend="language.oop5">classes and objects</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">escaping

Modified: phpdoc/en/trunk/language/oop5/basic.xml
===================================================================
--- phpdoc/en/trunk/language/oop5/basic.xml	2020-11-28 22:49:17 UTC (rev 351734)
+++ phpdoc/en/trunk/language/oop5/basic.xml	2020-11-28 23:09:30 UTC (rev 351735)
@@ -493,8 +493,56 @@
     </screen>
    </example>
   </sect2>
- </sect1>
+ <sect2 xml:id="language.oop5.basic.nullsafe">
+  <title>Nullsafe methods and properties</title>
+  <para>
+   As of PHP 8.0.0, properties and methods may also be accessed with the
+   "nullsafe" operator instead: <literal>?-></literal>. The nullsafe operator
+   works the same as property or method access as above, except that if the
+   object being dereferenced is &null; then &null;
+   will be returned rather than an exception thrown. If the dereference is part of a
+   chain, the rest of the chain is skipped.
+  </para>
+  <para>
+   The effect is similar to wrapping each access in an <function>is_null</function>
+   check first, but more compact.
+  </para>
+  <para>
+   <example>
+    <title>Nullsafe Operator</title>
+    <programlisting role="php">
+<![CDATA[
+<?php

+// As of PHP 8.0.0, this line:
+$result = $repository?->getUser(5)?->name;
+
+// Is equivalent to the following code block:
+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>
+    The nullsafe operator is best used when null is considered a valid and expected
+    possible value for a property or method return. For indicating an error,
+    a thrown exception is preferable.
+   </para>
+  </note>
+ </sect2>
+</sect1>
+
 <!-- Keep this comment at the end of the file
 Local variables:
 mode: sgml



-- 
PHP Documentation Commits Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic