update : 2015.11.03
php.shukuma.com검색:
|
Backward incompatible changesChanges to error and exception handlingMany fatal and recoverable fatal errors have been converted to exceptions in PHP 7. These error exceptions inherit from the Error class, which itself implements the Throwable interface (the new base interface all exceptions inherit). This means that custom error handlers may no longer be triggered because exceptions may be thrown instead (causing new fatal errors for uncaught Error exceptions). A fuller description of how errors operate in PHP 7 can be found on the PHP 7 errors page. This migration guide will merely enumerate the changes that affect backward compatibility. Internal constructors always throw exceptions on failure
Previously, some internal classes would return Parse errors throw ParseErrorParser errors now throw a ParseError object. Error handling for eval() should now include a catch block that can handle this error. E_STRICT notices severity changes
All of the
Changes to variable handlingPHP 7 now uses an abstract syntax tree when parsing source files. This has permitted many improvements to the language which were previously impossible due to limitations in the parser used in earlier versions of PHP, but has resulted in the removal of a few special cases for consistency reasons, which has resulted in backward compatibility breaks. These cases are detailed in this section. Changes to the handling of indirect variables, properties, and methodsIndirect access to variables, properties, and methods will now be evaluated strictly in left-to-right order, as opposed to the previous mix of special cases. The table below shows how the order of evaluaiton has changed.
Code that used the old right-to-left evaluation order must be rewritten to explicitly use that evaluation order with curly braces (see the above middle column). This will make the code both forwards compatible with PHP 7.x and backwards compatible with PHP 5.x. Changes to list() handlinglist() no longer assigns variables in reverse order
list() will now assign values to variables in the
order they are defined, rather than reverse order. In general, this only
affects the case where list() is being used in
conjunction with the array
<?php Output of the above example in PHP 5: array(3) { [0]=> int(3) [1]=> int(2) [2]=> int(1) } Output of the above example in PHP 7: array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } In general, it is recommended not to rely on the order in which list() assignments occur, as this is an implementation detail that may change again in the future. Empty list() assignments have been removedlist() constructs can no longer be empty. The following are no longer allowed:
<?php list() cannot unpack stringslist() can no longer unpack string variables. str_split() should be used instead. Array ordering when elements are automatically created during by reference assignments has changedThe order of the elements in an array has changed when those elements have been automatically created by referencing them in a by reference assignment. For example:
<?php Output of the above example in PHP 5: array(2) { ["b"]=> &int(1) ["a"]=> &int(1) } Output of the above example in PHP 7: array(2) { ["a"]=> &int(1) ["b"]=> &int(1) } global only accepts simple variablesVariable variables can no longer be used with the global keyword. The curly brace syntax can be used to emulate the previous behaviour if required:
<?php As a general principle, using anything other than a bare variable with global is discouraged. Parentheses around function parameters no longer affect behaviourIn PHP 5, using redundant parentheses around a function parameter could quiet strict standards warnings when the function parameter was passed by reference. The warning will now always be issued.
<?php 위 예제의 출력: Notice: Only variables should be passed by reference in /tmp/test.php on line 13 Changes to foreachMinor changes have been made to the behaviour of the foreach control structure, primarily around the handling of the internal array pointer and modification of the array being iterated over. foreach no longer changes the internal array pointerPrior to PHP 7, the internal array pointer was modified while an array was being iterated over with foreach. This is no longer the case, as shown in the following example:
<?php Output of the above example in PHP 5: int(1) int(2) bool(false) Output of the above example in PHP 7: int(0) int(0) int(0) foreach by-value operates on a copy of the arrayWhen used in the default by-value mode, foreach will now operate on a copy of the array being iterated rather than the array itself. This means that changes to the array made during iteration will not affect the values that are iterated. foreach by-reference has improved iteration behaviourWhen iterating by-reference, foreach will now do a better job of tracking changes to the array made during iteration. For example, appending to an array while iterating will now result in the appended values being iterated over as well:
<?php Output of the above example in PHP 5: int(0) Output of the above example in PHP 7: int(0) int(1) Iteration of non-Traversable objectsIterating over a non-Traversable object will now have the same behaviour as iterating over by-reference arrays. This results in the improved behaviour when modifying an array during iteration also being applied when properties are added to or removed from the object. Changes to integer handlingInvalid octal literalsPreviously, octal literals that contained invalid numbers were silently truncated (0128 was taken as 012). Now, an invalid octal literal will cause a parse error. Negative bitshiftsBitwise shifts by negative numbers will now throw an ArithmeticError:
<?php Output of the above example in PHP 5: int(0) Output of the above example in PHP 7: Fatal error: Uncaught ArithmeticError: Bit shift by negative number in /tmp/test.php:2 Stack trace: #0 {main} thrown in /tmp/test.php on line 2 Out of range bitshiftsBitwise shifts (in either direction) beyond the bit width of an integer will always result in 0. Previously, the behaviour of such shifts was architecture dependent. Changes to Division By Zero
Previously, when 0 was used as the divisor for either the divide (/) or
modulus (%) operators, an E_WARNING would be emitted and
<?php Output of the above example in PHP 5: Warning: Division by zero in %s on line %d bool(false) Warning: Division by zero in %s on line %d bool(false) Warning: Division by zero in %s on line %d bool(false) Output of the above example in PHP 7: Warning: Division by zero in %s on line %d float(INF) Warning: Division by zero in %s on line %d float(NAN) PHP Fatal error: Uncaught DivisionByZeroError: Modulo by zero in %s line %d Changes to string handlingHexadecimal strings are no longer considered numericStrings containing hexadecimal numbers are no longer considered to be numeric. For example:
<?php Output of the above example in PHP 5: bool(true) bool(true) int(15) string(2) "oo" Output of the above example in PHP 7: bool(false) bool(false) int(0) Notice: A non well formed numeric value encountered in /tmp/test.php on line 5 string(3) "foo" filter_var() can be used to check if a string contains a hexadecimal number, and also to convert a string of that type to an integer:
<?php \u{ may cause errorsDue to the addition of the new Unicode codepoint escape syntax, strings containing a literal \u{ followed by an invalid sequence will cause a fatal error. To avoid this, the leading backslash should be escaped. Removed functionscall_user_method() and call_user_method_array()These functions were deprecated in PHP 4.1.0 in favour of call_user_func() and call_user_func_array(). You may also want to consider using variable functions and/or the ... operator. mcrypt aliasesThe deprecated mcrypt_generic_end() function has been removed in favour of mcrypt_generic_deinit().
Additionally, the deprecated mcrypt_ecb(),
mcrypt_cbc(), mcrypt_cfb() and
mcrypt_ofb() functions have been removed in favour of
using mcrypt_decrypt() with the appropriate
intl aliasesThe deprecated datefmt_set_timezone_id() and IntlDateFormatter::setTimeZoneID() aliases have been removed in favour of datefmt_set_timezone() and IntlDateFormatter::setTimeZone(), respectively. set_magic_quotes_runtime()set_magic_quotes_runtime(), along with its alias magic_quotes_runtime(), have been removed. They were deprecated in PHP 5.3.0, and became effectively non-functional with the removal of magic quotes in PHP 5.4.0. set_socket_blocking()The deprecated set_socket_blocking() alias has been removed in favour of stream_set_blocking(). dl() in PHP-FPMdl() can no longer be used in PHP-FPM. It remains functional in the CLI and embed SAPIs. GD Type1 functionsSupport for PostScript Type1 fonts has been removed from the GD extension, resulting in the removal of the following functions:
Using TrueType fonts and their associated functions is recommended instead. Removed INI directivesRemoved featuresThe following INI directives have been removed as their associated features have also been removed:
|
Opening tag | Closing tag |
---|---|
<% |
%> |
<%= |
%> |
<script language="php"> |
</script> |
Previously deprecated in PHP 5.6, static calls made to a non-static method with an incompatible context will now result in the called method having an undefined $this variable and a deprecation warning being issued.
<?php
class A {
public function test() { var_dump($this); }
}
// Note: Does NOT extend A
class B {
public function callNonStaticMethodOfA() { A::test(); }
}
(new B)->callNonStaticMethodOfA();
?>
Output of the above example in PHP 5:
Deprecated: Non-static method A::test() should not be called statically, assuming $this from incompatible context in /tmp/test.php on line 8 object(B)#1 (0) { }
Output of the above example in PHP 7:
Deprecated: Non-static method A::test() should not be called statically in /tmp/test.php on line 8 Notice: Undefined variable: this in /tmp/test.php on line 3 NULL
The yield construct no longer requires parentheses, and has been changed to a right associative operator with precedence between print and =>. This can result in changed behaviour:
<?php
echo yield -1;
// Was previously interpreted as
echo (yield) - 1;
// And is now interpreted as
echo yield (-1);
yield $foo or die;
// Was previously interpreted as
yield ($foo or die);
// And is now interpreted as
(yield $foo) or die;
?>
Parentheses can be used to disambiguate those cases.
It is no longer possible to define two or more function parameters with the
same name. For example, the following function will trigger an
E_COMPILE_ERROR
:
<?php
function foo($a, $b, $unused, $unused) {
//
}
?>
It is no longer possible to define two or more default blocks in a switch
statement. For example, the following will switch statement will trigger an
E_COMPILE_ERROR
:
<?php
switch (1) {
default:
break;
default:
break;
}
?>
$HTTP_RAW_POST_DATA is no longer available. The php://input stream should be used instead.
Support for prefixing comments with # in INI files has been removed. ; (semi-colon) should be used instead. This change applies to php.ini, as well as files handled by parse_ini_file() and parse_ini_string().