mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-15 16:38:54 +00:00
Move scalar and return typing up, since that's probably what people care about.
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@337363 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
ef362e88b3
commit
a464d22608
1 changed files with 96 additions and 95 deletions
|
@ -4,101 +4,6 @@
|
|||
<sect1 xml:id="migration70.new-features" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<title>New features</title>
|
||||
|
||||
<sect2 xml:id="migration70.new-features.null-coalesce-op">
|
||||
<title>Null coalesce operator</title>
|
||||
|
||||
<para>
|
||||
The null coalesce operator (<literal>??</literal>) has been added as
|
||||
syntactic sugar for the common case of needing to use a ternary in
|
||||
conjunction with <function>isset</function>. It returns its first operand
|
||||
if it exists and is not &null;; otherwise it returns its second operand.
|
||||
</para>
|
||||
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
// Fetches the value of $_GET['user'] and returns 'nobody'
|
||||
// if it does not exist.
|
||||
$username = $_GET['user'] ?? 'nobody';
|
||||
// This is equivalent to:
|
||||
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
|
||||
|
||||
// Coalesces can be chained: this will return the first
|
||||
// defined value out of $_GET['user'], $_POST['user'], and
|
||||
// 'nobody'.
|
||||
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
|
||||
<!-- TODO: we don't need further details here, but this still has to be
|
||||
added to the operator precedence table -->
|
||||
</sect2>
|
||||
|
||||
<sect2 xml:id="migration70.new-features.spaceship-op">
|
||||
<title>Spaceship operator</title>
|
||||
<para>
|
||||
The spaceship operator is used for comparing two expressions. It returns an
|
||||
<type>integer</type> less than, equal to, or greater than zero when
|
||||
<varname>$a</varname> is respectively less than, equal to, or greater than
|
||||
<varname>$b</varname>. Comparisons are performed according to PHP's usual
|
||||
<link linkend="types.comparisons">type comparison rules</link>.
|
||||
</para>
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
// Integers
|
||||
echo 1 <=> 1; // 0
|
||||
echo 1 <=> 2; // -1
|
||||
echo 2 <=> 1; // 1
|
||||
|
||||
// Floats
|
||||
echo 1.5 <=> 1.5; // 0
|
||||
echo 1.5 <=> 2.5; // -1
|
||||
echo 2.5 <=> 1.5; // 1
|
||||
|
||||
// Strings
|
||||
echo "a" <=> "a"; // 0
|
||||
echo "a" <=> "b"; // -1
|
||||
echo "b" <=> "a"; // 1
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
|
||||
<!-- TODO: we don't need further details here, but this still has to be
|
||||
added to the operator precedence table -->
|
||||
</sect2>
|
||||
|
||||
<sect2 xml:id="migration70.new-features.define-array">
|
||||
<title>Constant arrays using <function>define</function></title>
|
||||
|
||||
<para>
|
||||
<type>Array</type> constants can now be defined with
|
||||
<function>define</function>. In PHP 5.6, they could only be defined with
|
||||
&const;.
|
||||
</para>
|
||||
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
define('ANIMALS', [
|
||||
'dog',
|
||||
'cat',
|
||||
'bird'
|
||||
]);
|
||||
|
||||
echo ANIMALS[1]; // outputs "cat"
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
</sect2>
|
||||
|
||||
<sect2 xml:id="migration70.new-features.scalar-type-declarations">
|
||||
<title>Scalar Type Declarations</title>
|
||||
|
||||
|
@ -187,6 +92,7 @@ Fatal error: Uncaught TypeError: Argument 1 passed to add() must be of the type
|
|||
will apply.
|
||||
</para>
|
||||
</sect2>
|
||||
|
||||
<sect2 xml:id="migration70.new-features.return-type-declarations">
|
||||
<title>Return Type Declarations</title>
|
||||
|
||||
|
@ -312,6 +218,101 @@ Fatal error: Uncaught TypeError: Return value of B::test() must be an instance o
|
|||
not a valid return type - only an instance of the class A can be returned.
|
||||
</para>
|
||||
</sect2>
|
||||
|
||||
<sect2 xml:id="migration70.new-features.null-coalesce-op">
|
||||
<title>Null coalesce operator</title>
|
||||
|
||||
<para>
|
||||
The null coalesce operator (<literal>??</literal>) has been added as
|
||||
syntactic sugar for the common case of needing to use a ternary in
|
||||
conjunction with <function>isset</function>. It returns its first operand
|
||||
if it exists and is not &null;; otherwise it returns its second operand.
|
||||
</para>
|
||||
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
// Fetches the value of $_GET['user'] and returns 'nobody'
|
||||
// if it does not exist.
|
||||
$username = $_GET['user'] ?? 'nobody';
|
||||
// This is equivalent to:
|
||||
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
|
||||
|
||||
// Coalesces can be chained: this will return the first
|
||||
// defined value out of $_GET['user'], $_POST['user'], and
|
||||
// 'nobody'.
|
||||
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
|
||||
<!-- TODO: we don't need further details here, but this still has to be
|
||||
added to the operator precedence table -->
|
||||
</sect2>
|
||||
|
||||
<sect2 xml:id="migration70.new-features.spaceship-op">
|
||||
<title>Spaceship operator</title>
|
||||
<para>
|
||||
The spaceship operator is used for comparing two expressions. It returns an
|
||||
<type>integer</type> less than, equal to, or greater than zero when
|
||||
<varname>$a</varname> is respectively less than, equal to, or greater than
|
||||
<varname>$b</varname>. Comparisons are performed according to PHP's usual
|
||||
<link linkend="types.comparisons">type comparison rules</link>.
|
||||
</para>
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
// Integers
|
||||
echo 1 <=> 1; // 0
|
||||
echo 1 <=> 2; // -1
|
||||
echo 2 <=> 1; // 1
|
||||
|
||||
// Floats
|
||||
echo 1.5 <=> 1.5; // 0
|
||||
echo 1.5 <=> 2.5; // -1
|
||||
echo 2.5 <=> 1.5; // 1
|
||||
|
||||
// Strings
|
||||
echo "a" <=> "a"; // 0
|
||||
echo "a" <=> "b"; // -1
|
||||
echo "b" <=> "a"; // 1
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
|
||||
<!-- TODO: we don't need further details here, but this still has to be
|
||||
added to the operator precedence table -->
|
||||
</sect2>
|
||||
|
||||
<sect2 xml:id="migration70.new-features.define-array">
|
||||
<title>Constant arrays using <function>define</function></title>
|
||||
|
||||
<para>
|
||||
<type>Array</type> constants can now be defined with
|
||||
<function>define</function>. In PHP 5.6, they could only be defined with
|
||||
&const;.
|
||||
</para>
|
||||
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
define('ANIMALS', [
|
||||
'dog',
|
||||
'cat',
|
||||
'bird'
|
||||
]);
|
||||
|
||||
echo ANIMALS[1]; // outputs "cat"
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
</sect2>
|
||||
<sect2 xml:id="migration70.new-features.anonymous-classes">
|
||||
<title>Anonymous Classes</title>
|
||||
|
||||
|
|
Loading…
Reference in a new issue