php-doc-en/appendices/migration71/new-features.xml
Christoph Michael Becker 459c0e1de4 Fix #75414: tcp_nodelay option for stream context
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@344750 c90b9560-bf6c-de11-be94-00142212c4b1
2018-04-19 17:04:53 +00:00

434 lines
10 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<sect1 xml:id="migration71.new-features" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>New features</title>
<sect2 xml:id="migration71.new-features.nullable-types">
<title>Nullable types</title>
<para>
Type declarations for parameters and return values can now be marked as
nullable by prefixing the type name with a question mark. This signifies
that as well as the specified type, &null; can be passed as an argument, or
returned as a value, respectively.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
function testReturn(): ?string
{
return 'elePHPant';
}
var_dump(testReturn());
function testReturn(): ?string
{
return null;
}
var_dump(testReturn());
function test(?string $name)
{
var_dump($name);
}
test('elePHPant');
test(null);
test();
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(10) "elePHPant"
NULL
string(10) "elePHPant"
NULL
Uncaught Error: Too few arguments to function test(), 0 passed in...
]]>
</screen>
</informalexample>
</sect2>
<sect2 xml:id="migration71.new-features.void-functions">
<title>Void functions</title>
<para>
A <type>void</type> return type has been introduced. Functions declared with
void as their return type must either omit their return statement altogether,
or use an empty return statement. &null; is not a valid return value for a
void function.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
function swap(&$left, &$right): void
{
if ($left === $right) {
return;
}
$tmp = $left;
$left = $right;
$right = $tmp;
}
$a = 1;
$b = 2;
var_dump(swap($a, $b), $a, $b);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
null
int(2)
int(1)
]]>
</screen>
</informalexample>
<para>
Attempting to use a void function's return value simply evaluates to
&null;, with no warnings emitted. The reason for this is because warnings
would implicate the use of generic higher order functions.
</para>
</sect2>
<sect2 xml:id="migration71.new-features.symmetric-array-destructuring">
<title>Symmetric array destructuring</title>
<para>
The shorthand array syntax (<literal>[]</literal>) may now be used to
destructure arrays for assignments (including within
<literal>foreach</literal>), as an alternative to the existing
<function>list</function> syntax, which is still supported.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$data = [
[1, 'Tom'],
[2, 'Fred'],
];
// list() style
list($id1, $name1) = $data[0];
// [] style
[$id1, $name1] = $data[0];
// list() style
foreach ($data as list($id, $name)) {
// logic here with $id and $name
}
// [] style
foreach ($data as [$id, $name]) {
// logic here with $id and $name
}
]]>
</programlisting>
</informalexample>
</sect2>
<sect2 xml:id="migration71.new-features.class-constant-visibility">
<title>Class constant visibility</title>
<para>
Support for specifying the visibility of class constants has been added.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class ConstDemo
{
const PUBLIC_CONST_A = 1;
public const PUBLIC_CONST_B = 2;
protected const PROTECTED_CONST = 3;
private const PRIVATE_CONST = 4;
}
]]>
</programlisting>
</informalexample>
</sect2>
<sect2 xml:id="migration71.new-features.iterable-pseudo-type">
<title><type>iterable</type> pseudo-type</title>
<para>
A new pseudo-type (similar to <type>callable</type>) called
<type>iterable</type> has been introduced. It may be used in parameter
and return types, where it accepts either arrays or objects that implement
the <classname>Traversable</classname> interface. With respect to subtyping,
parameter types of child classes may broaden a parent's declaration of <type>array</type>
or <classname>Traversable</classname> to <type>iterable</type>. With return types,
child classes may narrow a parent's return type of <type>iterable</type> to
<type>array</type> or an object that implements <classname>Traversable</classname>.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
function iterator(iterable $iter)
{
foreach ($iter as $val) {
//
}
}
]]>
</programlisting>
</informalexample>
</sect2>
<sect2 xml:id="migration71.new-features.mulit-catch-exception-handling">
<title>Multi catch exception handling</title>
<para>
Multiple exceptions per catch block may now be specified using the pipe
character (<literal>|</literal>). This is useful for when different
exceptions from different class hierarchies are handled the same.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
try {
// some code
} catch (FirstException | SecondException $e) {
// handle first and second exceptions
}
]]>
</programlisting>
</informalexample>
</sect2>
<sect2 xml:id="migration71.new-features.support-for-keys-in-list">
<title>Support for keys in <function>list</function></title>
<para>
You can now specify keys in <function>list</function>, or its new shorthand
<literal>[]</literal> syntax. This enables destructuring of arrays with
non-integer or non-sequential keys.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
// list() style
list("id" => $id1, "name" => $name1) = $data[0];
// [] style
["id" => $id1, "name" => $name1] = $data[0];
// list() style
foreach ($data as list("id" => $id, "name" => $name)) {
// logic here with $id and $name
}
// [] style
foreach ($data as ["id" => $id, "name" => $name]) {
// logic here with $id and $name
}
]]>
</programlisting>
</informalexample>
</sect2>
<sect2 xml:id="migration71.new-features.support-for-negative-string-offsets">
<title>Support for negative string offsets</title>
<para>
Support for negative string offsets has been added to the
<link linkend="book.strings">string manipulation functions</link>
accepting offsets, as well as to
<link linkend="language.types.string.substr">string indexing</link> with
<literal>[]</literal> or <literal>{}</literal>. In such cases, a negative
offset is interpreted as being an offset from the end of the string.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
var_dump("abcdef"[-2]);
var_dump(strpos("aabbcc", "b", -3));
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string (1) "e"
int(3)
]]>
</screen>
</informalexample>
<para>
Negative string and array offsets are now also supported in the simple
variable parsing syntax inside of strings.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$string = 'bar';
echo "The last character of '$string' is '$string[-1]'.\n";
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
The last character of 'bar' is 'r'.
]]>
</screen>
</informalexample>
</sect2>
<sect2 xml:id="migration71.new-features.support-for-aead-in-ext-openssl">
<title>Support for AEAD in ext/openssl</title>
<para>
Support for AEAD (modes GCM and CCM) have been added by extending the
<function>openssl_encrypt</function> and
<function>openssl_decrypt</function> functions with additional parameters.
</para>
</sect2>
<sect2 xml:id="migration71.new-features.convert-callables-to-closures">
<title>Convert callables to <classname>Closure</classname>s with <methodname>Closure::fromCallable</methodname></title>
<para>
A new static method has been introduced to the <classname>Closure</classname>
class to allow for <type>callable</type>s to be easily converted into
<classname>Closure</classname> objects.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class Test
{
public function exposeFunction()
{
return Closure::fromCallable([$this, 'privateFunction']);
}
private function privateFunction($param)
{
var_dump($param);
}
}
$privFunc = (new Test)->exposeFunction();
$privFunc('some value');
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(10) "some value"
]]>
</screen>
</informalexample>
</sect2>
<sect2 xml:id="migration71.new-features.asynchronous-signal-handling">
<title>Asynchronous signal handling</title>
<para>
A new function called <function>pcntl_async_signals</function> has been
introduced to enable asynchronous signal handling without using ticks (which
introduce a lot of overhead).
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
pcntl_async_signals(true); // turn on async signals
pcntl_signal(SIGHUP, function($sig) {
echo "SIGHUP\n";
});
posix_kill(posix_getpid(), SIGHUP);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
SIGHUP
]]>
</screen>
</informalexample>
</sect2>
<sect2 xml:id="migration71.new-features.http2-server-push-support-in-ext-curl">
<title>HTTP/2 server push support in ext/curl</title>
<para>
Support for server push has been added to the CURL extension (requires
version 7.46 and above). This can be leveraged through the
<function>curl_multi_setopt</function> function with the new
<constant>CURLMOPT_PUSHFUNCTION</constant> constant. The constants
<constant>CURL_PUSH_OK</constant> and <constant>CURL_PUSH_DENY</constant> have also been
added so that the execution of the server push callback can either be
approved or denied.
</para>
</sect2>
<sect2 xml:id="migration71.new-features.stream-context-options">
<title>Stream Context Options</title>
<para>
The <link linkend="context.socket.tcp_nodelay">tcp_nodelay</link> stream
context option has been added.
</para>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->