What's new in PHP 8.1
A few stats
$ git diff --shortstat 14806e08 HEAD
11037 files changed, 663672 insertions(+), 270931 deletions(-)
$ git rev-list --count --no-merges PHP-8.0..PHP-8.1
2279
$ git shortlog --summary --oneline --no-merges PHP-8.0..PHP-8.1|sort -r
816 Nikita Popov
418 Dmitry Stogov
172 Alex Dowad
128 Máté Kocsis
120 George Peter Banyard
79 Hao Sun
42 Anatol Belski
40 Christoph M. Becker
36 Joe Watkins
29 Aaron Piotrowski
28 Remi Collet
28 Kamil Tekiela
Branched on 2020-10-09
GA release on 2021-11-25
25 accepted RFCs
13 rejected RFCs
Highlighted
new features
Readonly Properties
class User
{
private readonly int $id;
private readonly string $email;
private readonly string $name;
}
Enumerations
enum Suit {
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
function pick_a_card(Suit $suit) {}
pick_a_card(Suit::Clubs);
New in Initializers
function test($foo = new A()) {}
First-Class Callables
$fn = Closure::fromCallable('strlen');
$fn = strlen(...);
$fn = Closure::fromCallable([$this, 'method']);
$fn = $this->method(...)
$fn = Closure::fromCallable([Foo::class, 'method']);
$fn = Foo::method(...);
Final Class Constants
class Foo
{
final public const X = "foo";
}
class Bar extends Foo
{
public const X = "bar";
}
// Fatal error: Bar::X cannot override final constant
// Foo::X
Fibers
Type System Improvements
Pure Intersection Types
class UserServiceTest extends TestCase
{
private UserRepository&MockObject $userRepository;
}
Never Type
function neverReturns1(): never
{
throw new Exception();
}
function neverReturns2(): never
{
exit();
}
Highlighted
Deprecations
Autovivification on False
// From undefined
$arr[] = 'some value';
$arr['doesNotExist'][] = 2;
// From null
$arr = null;
$arr[] = 2;
// From false
$arr = false;
$arr[] = 2;
// Automatic conversion of false to array is deprecated
Non-integer-compatible Float-Int Conversion
function foo(int $int) {}
foo(3.14);
// Implicit conversion from float 3.14 to int loses precision
Passing Null to Non-Nullable Internal Function Arguments
var_dump(str_contains("foobar", null));
// Passing null to argument of type string is deprecated
// bool(true)
Omitting Internal Method Return Types
class MyDateTime extends DateTime
{
public function modify(string $modifier) { return false; }
}
// Declaration of MyDateTime::modify(string $modifier) should be
// compatible with DateTime::modify(string $modifier): DateTime|false
What's new in PHP 8.1
By Máté Kocsis
What's new in PHP 8.1
- 395