-
match
现在是一个保留字。
-
断言(Assertion)失败现在默认抛出异常。如果想要改回之前的行为,可以在 INI 设置中设置
assert.exception=0
。
-
与类名相同的方法名将不再被当做构造方法。应该使用__construct()
来取代它。
-
不再允许通过静态调用的方式去调用非静态方法。因此is_callable()在检查一个类名与非静态方法
时将返回失败(应当检查一个类的实例)。
-
(real)
和 (unset)
转换已被移除。
-
The track_errors ini directive has been removed. This
means that php_errormsg is no longer available. The
error_get_last() function may be used instead.
-
The ability to define case-insensitive constants has been removed. The third argument to
define() may no longer be true
.
-
The ability to specify an autoloader using an __autoload() function has been
removed. spl_autoload_register() should be used instead.
-
The errcontext
argument will no longer be passed to custom error handlers
set with set_error_handler().
-
create_function() has been removed. Anonymous functions may be used instead.
-
each() has been removed. foreach or ArrayIterator
should be used instead.
-
The ability to unbind this from closures that were created from a method,
using Closure::fromCallable() or
ReflectionMethod::getClosure(), has been removed.
-
The ability to unbind this from proper closures that contain uses of
this has also been removed.
-
The ability to use array_key_exists() with objects has been removed.
isset() or property_exists() may be used instead.
-
The behavior of array_key_exists() regarding the type of the
key
parameter has been made consistent with isset() and
normal array access. All key types now use the usual coercions and array/object keys throw a
TypeError.
-
Any array that has a number n as its first numeric key will use
n+1 for its next implicit key, even if n is
negative.
-
The default error_reporting level is now E_ALL
. Previously it excluded
E_NOTICE
and E_DEPRECATED
.
-
display_startup_errors is now enabled by
default.
-
Using parent inside a class that has no parent will now result in a fatal
compile-time error.
-
The @
operator will no longer silence fatal errors
(E_ERROR
, E_CORE_ERROR
,
E_COMPILE_ERROR
, E_USER_ERROR
,
E_RECOVERABLE_ERROR
, E_PARSE
). Error handlers that
expect error_reporting to be 0
when @
is used, should be
adjusted to use a mask check instead:
<?php
// Replace
function my_error_handler($err_no, $err_msg, $filename, $linenum) {
if (error_reporting() == 0) {
return false; // Silenced
}
// ...
}
// With
function my_error_handler($err_no, $err_msg, $filename, $linenum) {
if (!(error_reporting() & $err_no)) {
return false; // Silenced
}
// ...
}
?>
Additionally, care should be taken that error messages are not displayed in production
environments, which can result in information leaks. Please ensure that
display_errors=Off
is used in conjunction with error logging.
-
#[
is no longer interpreted as the start of a comment,
as this syntax is now used for attributes.
-
Inheritance errors due to incompatible method signatures (LSP violations) will now always
generate a fatal error. Previously a warning was generated in some cases.
-
The precedence of the concatenation operator has changed relative to bitshifts and addition as
well as subtraction.
<?php
echo "Sum: " . $a + $b;
// was previously interpreted as:
echo ("Sum: " . $a) + $b;
// is now interpreted as:
echo "Sum:" . ($a + $b);
?>
-
Arguments with a default value that resolves to null
at runtime will no longer implicitly mark
the argument type as nullable. Either an explicit nullable type, or an explicit null
default
value has to be used instead.
<?php
// Replace
function test(int $arg = CONST_RESOLVING_TO_NULL) {}
// With
function test(?int $arg = CONST_RESOLVING_TO_NULL) {}
// Or
function test(int $arg = null) {}
?>
-
A number of warnings have been converted into Error exceptions:
-
Attempting to write to a property of a non-object. Previously this
implicitly created an stdClass object for null, false and empty strings.
-
Attempting to append an element to an array for which the PHP_INT_MAX key
is already used.
-
Attempting to use an invalid type (array or object) as an array key or
string offset.
- Attempting to write to an array index of a scalar value.
- Attempting to unpack a non-array/Traversable.
-
Attempting to access unqualified constants which are undefined.
Previously, unqualified constant accesses resulted in a warning and were interpreted as strings.
A number of notices have been converted into warnings:
- Attempting to read an undefined variable.
- Attempting to read an undefined property.
- Attempting to read an undefined array key.
- Attempting to read a property of a non-object.
- Attempting to access an array index of a non-array.
- Attempting to convert an array to string.
- Attempting to use a resource as an array key.
- Attempting to use null, a boolean, or a float as a string offset.
- Attempting to read an out-of-bounds string offset.
- Attempting to assign an empty string to a string offset.
-
Attempting to assign multiple bytes to a string offset will now emit a warning.
-
Unexpected characters in source files (such as NUL bytes outside of strings) will now result in a
ParseError exception instead of a compile warning.
-
Uncaught exceptions now go through "clean shutdown", which means that destructors will be called
after an uncaught exception.
-
The compile time fatal error "Only variables can be passed by reference" has been delayed until
runtime, and converted into an "Argument cannot be passed by reference"
Error exception.
-
Some "Only variables should be passed by reference" notices have been converted to "Argument
cannot be passed by reference" exception.
-
The generated name for anonymous classes has changed. It will now include the name of the first
parent or interface:
<?php
new class extends ParentClass {};
// -> ParentClass@anonymous
new class implements FirstInterface, SecondInterface {};
// -> FirstInterface@anonymous
new class {};
// -> class@anonymous
?>
The name shown above is still followed by a NUL byte and a unique suffix.
-
Non-absolute trait method references in trait alias adaptations are now required to be
unambiguous:
<?php
class X {
use T1, T2 {
func as otherFunc;
}
function func() {}
}
?>
If both T1::func()
and T2::func()
exist, this code was previously
silently accepted, and func was assumed to refer to T1::func
. Now it will generate a
fatal error instead, and either T1::func
or T2::func
needs to be
written explicitly.
-
The signature of abstract methods defined in traits is now checked against the implementing class
method:
<?php
trait MyTrait {
abstract private function neededByTrait(): string;
}
class MyClass {
use MyTrait;
// Error, because of return type mismatch.
private function neededByTrait(): int { return 42; }
}
?>
-
Disabled functions are now treated exactly like non-existent functions. Calling a disabled
function will report it as unknown, and redefining a disabled function is now possible.
-
data://
stream wrappers are no longer writable, which matches the documented
behavior.
-
The arithmetic and bitwise operators +
, -
,
*
, /
, **
, %
,
<<
, >>
, &
,
|
, ^
, ~
, ++
,
--
will now consistently throw a TypeError when one of
the operands is an array, 资源(resource) or non-overloaded object. The only exception to this is
the array +
array merge operation, which remains supported.
-
Float to string casting will now always behave locale-independently.
<?php
setlocale(LC_ALL, "de_DE");
$f = 3.14;
echo $f, "\n";
// Previously: 3,14
// Now: 3.14
?>
See printf(), number_format() and
NumberFormatter() for ways to customize number formatting.
-
Support for deprecated curly braces for offset access has been removed.
<?php
// Instead of:
$array{0};
$array{"key"};
// Write:
$array[0];
$array["key"];
?>
-
Applying the final modifier on a private method will now produce a warning unless that method is
the constructor.
-
If an object constructor exit()s, the object destructor will no longer be
called. This matches the behavior when the constructor throws.
-
Namespaced names can no longer contain whitespace: While Foo\Bar
will be recognized
as a namespaced name, Foo \ Bar
will not. Conversely, reserved keywords are now
permitted as namespace segments, which may also change the interpretation of code:
new\x
is now the same as constant('new\x')
, not
new \x()
.
-
Nested ternaries now require explicit parentheses.
-
debug_backtrace() and Exception::getTrace() will no
longer provide references to arguments. It will not be possible to change function arguments
through the backtrace.
-
Numeric string handling has been altered to be more intuitive and less error-prone. Trailing
whitespace is now allowed in numeric strings for consistency with how leading whitespace is
treated. This mostly affects:
- The is_numeric() function
- String-to-string comparisons
- Type declarations
- Increment and decrement operations
The concept of a "leading-numeric string" has been mostly dropped; the cases where this remains
exist in order to ease migration. Strings which emitted an E_NOTICE
"A non
well-formed numeric value encountered" will now emit an E_WARNING
"A
non-numeric value encountered" and all strings which emitted an E_WARNING
"A
non-numeric value encountered" will now throw a
TypeError. This mostly affects:
- Arithmetic operations
- Bitwise operations
This E_WARNING
to TypeError change also affects the
E_WARNING
"Illegal string offset 'string'" for illegal string offsets. The
behavior of explicit casts to int/float from strings has not been changed.
-
Magic Methods will now have their arguments and return types checked if they have them declared.
The signatures should match the following list:
__call(string $name, array $arguments): mixed
__callStatic(string $name, array $arguments): mixed
__clone(): void
__debugInfo(): ?array
__get(string $name): mixed
__invoke(mixed $arguments): mixed
__isset(string $name): bool
__serialize(): array
__set(string $name, mixed $value): void
__set_state(array $properties): object
__sleep(): array
__unserialize(array $data): void
__unset(string $name): void
__wakeup(): void
-
call_user_func_array() array keys will now be interpreted as parameter names,
instead of being silently ignored.
-
Declaring a function called assert()
inside a namespace is
no longer allowed, and issues E_COMPILE_ERROR
.
The assert() function is subject to special handling by the engine,
which may lead to inconsistent behavior when defining a namespaced function with the same name.