Skip to content

Instantly share code, notes, and snippets.

@lewisgoddard
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lewisgoddard/d58647557e4bb3b6d501 to your computer and use it in GitHub Desktop.
Save lewisgoddard/d58647557e4bb3b6d501 to your computer and use it in GitHub Desktop.
Planning for PHP 7

Breaking Changes

Variables only inputs are stricter.

// Strict Standards: Only variables should be passed by reference
$last = array_pop(getArray());
$last = array_pop((getArray()));
  • Now errors on both calls.
  • Previously, there was no error on the second call.

Foreach() doesn't move the array pointer.

$array = [0, 1, 2];
foreach ($array as &$val) {
	var_dump(current($array));
}
  • Prints the value int(0) three times.
  • Previously the output was int(1), int(2), and bool(false).

Foreach() will operate on a copy of the array for values

$array = [0, 1, 2];
$ref =& $array; // Necessary to trigger the old behavior
foreach ($array as $val) {
  var_dump($val);
  unset($array[1]);
}
  • Prints all three elements (0 1 2)
  • Previously the second element was skipped (0 2).

Foreach() will honour array changes by reference

$array = [0];
foreach ($array as &$val) {
	var_dump($val);
	$array[1] = 1;
}
  • Now iterates over the appended element as well. As such the output of this example will now be "int(0) int(1)".
  • Previously it was only "int(0)".

Disallows duplicate parameter names when defining a function.

function foo($a, $b, $unused, $unused) {
	// ...
}
  • Now triggers a compile-time error.
  • Previously, I would have been amazed this worked.

Arguments references get updated

function foo($x) {
	$x++;
	var_dump(func_get_arg(0));
}
foo(1);
  • Now prints "2" instead of "1".

Backtraces will show updated values

function foo($x) {
	$x = 42;
	throw new Exception;
}
foo("string");
Now
Stack trace:
	#0 file.php(4): foo(42)
	#1 {main}
Previously
Stack trace:
	#0 file.php(4): foo('string')
	#1 {main}

Invalid octal literals (containing digits larger than 7) now produce compile errors.

$i = 0781; // 8 is not a valid octal digit!
  • Now compile-errors.
  • Previously ignored digits past anything invalid, resulting in 7.

Exceptions and Errors

There are now two exception classes: Exception and Error. Both classes implement a new interface Throwable. Type hints in exception handling code may need to be changed to account for this. Some fatal errors and recoverable fatal errors now throw an Error instead. As Error is a separate class from Exception, these exceptions will not be caught by existing try/catch blocks.

Removed ASP (<%) and script (<script language=php>) tags.

Removed support for #-style comments in ini files.

  • Use ;-style comments instead.

$HTTP_RAW_POST_DATA is no longer available.

  • Use the php://input stream instead

call_user_method() and call_user_method_array() no longer exists.

ob_start() no longer issues an E_ERROR, but instead an E_RECOVERABLE_ERROR in case an output buffer is created in an output buffer handler

setcookie() with an empty cookie name now issues a WARNING and doesn't send an empty set-cookie header line anymore

Removed support for disabling the CURLOPT_SAFE_UPLOAD option.

  • All curl file uploads must use the curl_file / CURLFile APIs.

Mcrypt

  • Removed deprecated mcrypt_generic_end() alias in favor of mcrypt_generic_deinit().
  • Removed deprecated mcrypt_ecb(), mcrypt_cbc(), mcrypt_cfb() and mcrypt_ofb() functions in favor of mcrypt_encrypt() and mcrypt_decrypt() with an MCRYPT_MODE_* flag.

OpenSSL:

  • Removed the "rsa_key_size" SSL context option in favor of automatically setting the appropriate size given the negotiated crypto algorithm.
  • Removed "CN_match" and "SNI_server_name" SSL context options. Use automatic detection or the "peer_name" option instead.

JSON:

  • Rejected RFC 7159 incompatible number formats in json_decode
    • string top level (07, 0xff, .1, -.1)
    • all levels ([1.], [1.e1])
  • Calling json_decode with 1st argument equal to empty PHP string or value that after casting to string is empty string (NULL, FALSE) results in JSON syntax error.

Improved performance: PHP 7 is up to twice as fast as PHP 5.6

Consistent 64-bit support

Many fatal errors are now Exceptions

Removal of old and unsupported SAPIs and extensions

  • MySQL

FPM

  • Fixed bug #65933 (Cannot specify config lines longer than 1024 bytes).
  • Listen = port now listen on all addresses (IPv6 and IPv4-mapped).

The null coalescing operator (??)

https://wiki.php.net/rfc/isset_ternary

$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
$username = $_GET['user'] ?? 'nobody';

Return Type Declarations

Scalar Type Declarations

Anonymous Classes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment