// PHP 7 /** * @var int|float $number */ private $number; // PHP 8 private int|float $number;
// PHP 7 /** * @var mixed $number */ private $number; // PHP 8 private mixed $number;
// PHP 7 htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false); // PHP 8 htmlspecialchars($string, double_encode: false);
use App\Attributes\ExampleAttribute; #[ExampleAttribute] class Foo { #[ExampleAttribute] public const FOO = 'foo'; #[ExampleAttribute] public $x; #[ExampleAttribute] public function foo(#[ExampleAttribute] $bar) { } } #[Attribute] class ExampleAttribute { public $value; public function __construct($value) { $this->value = $value; } }
// PHP 7 class Point { public float $x; public float $y; public function __construct( float $x = 0.0, float $y = 0.0, ) { $this->x = $x; $this->y = $y; } } // PHP 8 class Point { public function __construct( public float $x = 0.0, public float $y = 0.0, ) { // } }
switch (1) { case 0: $result = 'Foo'; break; case 1: $result = 'Bar'; break; case 2: $result = 'Baz'; break; } echo $result; // PHP 8 echo match (1) { 0 => 'Foo', 1 => 'Bar', 2 => 'Baz', };
// PHP 7 $country = null; if ($session !== null) { $user = $session->user; if ($user !== null) { $address = $user->getAddress(); if ($address !== null) { $country = $address->country; } } } // PHP 8 $country = $session?->user?->getAddress()?->country;
trait T { abstract public function test(): string; }
class C { use T; public function test() // Allowed in PHP 7 { // } public function test(): int // Forbidden in PHP 8 { // } public function test(): string // Allowed in PHP 8 { // } }
try { // } catch (Exception $exception){ // Unused variable // }
try { // } catch (Exception){ // }
$map = new WeakMap; $obj = new stdClass; $map[$obj] = 42; var_dump($map); // object(WeakMap)#1 (1) { // [0]=> // array(2) { // ["key"]=> // object(stdClass)#2 (0) { // } // ["value"]=> // int(42) // } // } // The object is destroyed here, and the key is automatically removed from the weak map. unset($obj); var_dump($map); // object(WeakMap)#1 (0) { // }
JIT makes bench.php more than two times faster: 0.140 sec vs 0.320 sec. It is expected to make most CPU-intensive workloads run significantly faster.
According to Nikita, PHP-Parser became ~1.3 times faster with JIT. Amphp hello-world.php got just 5% speedup.
However, like the previous attempts - it currently doesn't seem to significantly improve real-life apps like WordPress (with opcache.jit=1235 326 req/sec vs 315 req/sec).
It's planned to provide additional effort, improving JIT for real-life apps, using profiling and speculative optimizations.
Dutch Coding Company is een web & app projectstudio. Wij ontwikkelen kwalitatieve apps op maat voor iPhone en Android. Als technische partner maken en onderhouden wij apps voor bedrijven, netwerken, evenementen en innovatieve startups. Ons motto: web & app met striking impact.
Ons teamlid Bjorn Voesten is de auteur van dit artikel en kan ook vragen beantwoorden over volgende projecten. Bel hem op of stuur een email naar bjorn@dcc.team.