Вы находитесь на странице: 1из 2

CHEAT SHEETS / PHP / JAN 2017 / PAGE 1

PHP 7.1 Cheat Sheet


Thomas Punt, Author Array Destructuring on Assignment

Two new abilities have been introduced:


Expanding PHPs Type System list now supports keys, enabling the
destructuring of associative arrays
PHP 7.1 has expanded upon the type system Shorthand array syntax ([]) can now be used
introduced in PHP7 with these new abilities: to destructure arrays symmetrically

void: declare functions as having no return (not


$lines = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]];
even null)

foreach ($lines as [$from, $to]) {


function swap(int &$a, int &$b) : void { [$fromX, $fromY] = $from;
if ($a === $b) { [$toX, $toY] = $to;
return;
} // $fromX, $fromY, $toX, $toY
$tmp = $a; }
$a = $b;
$b = $tmp;
}
Class Constant Visibility

iterable: declare functions as accepting or Constants declared within classes may now
returning either a traversable or array specify their visibility

function iterator(iterable $iter) { class Example {


foreach ($iter as $it) { const PUBLIC_1 = 1;
public const PUBLIC_2 = 2;
} protected const PROTECTED_3 = 3;
} private const PRIVATE_4 = 4;
}
?: nullify a type (i.e. enable for either that type or
null) by prepending a question mark to the type
Multi-Catch Exception Handling
function username(?User $user) : string {
return $user ? $user->username : Guest; Multiple exceptions may now be specied inside
} catch statements.

try {

About the Author
} catch (AnException | AnotherException $e) {
Thomas is a Web Technologies graduate from the UK. He
has a vehement interest in programming, with particular }
focus on server-side web technologies like PHP and Elixir.
CHEAT SHEETS / PHP / JAN 2017 / PAGE 2

PHP 7.1 Cheat Sheet


Thomas Punt, Author Negative String Offsets

Negative string offsets can now be used with the


PCNTL Asynchronous Signals subset operator ([]) and string-based functions
that accept an offset parameter.
A new function, pcntl_async_signals, has been
introduced to enable asynchronous signal
var_dump(str[-1]); // string(1) r
handling in the PCNTL extension more easily.
var_dump(grapheme_extract(str, 1, 1, -1)); // string(1) r

pcntl_async_signals(true); // turn on async signals

pcntl_signal(SIGHUP, function($sig) { CURL Improvements


echo SIGHUP\n;
}); HTTP/2 server push support has been added to
the CURL extension with improved error handling
posix_kill(posix_getpid(), SIGHUP); for shared and multi-handled connections,
including the addition of the following:

Support for AEAD in OpenSSL int curl_multi_errno(resource $mh);


int curl_share_errno(resource $rh);
Support for AEAD (GCM and CCM modes) have string curl_share_strerror(int $errno);
been added by extending the openssl_encrypt and
openssl_decrypt functions with additional params.
Common Upgrade Problems (Continued)

Common Upgrade Problems Funcs. that can no longer be dynamically invoked:

Arithmetic on invalid strings will emit either an assert() - with a string as the rst argument
E_WARNING or an E_NOTICE, for example: compact()
extract()
1b + something; func_get_args()
// E_NOTICE for 1b, E_WARNING for something func_get_arg()
func_num_args()
Overow on octals will now emit an E_WARNING get_dened_vars()
mcrypt extension has now been deprecated mb_parse_str() - with one arg
Eval option for mb_ereg(i)_replace() deprecated parse_str() - with one arg
DateTime extension has added support for
microseconds, which will break code with Calling a user-dened function with too few args
already broken logic, for example: will now result in an ArgumentCountError exception
JSON decoding an empty key results in an empty
new DateTime == new DateTime; property name, not _empty_ as a property name
// will likely eval to false rather than being mostly true

Вам также может понравиться