Key New Features in PHP 8: JIT, Union Types, Attributes, Static Return Types, and WeakMap
The article introduces PHP 8’s major release scheduled for November 26, highlighting its most impactful additions such as Just‑In‑Time compilation, Union Types, Attributes, a new static return type, and WeakMap support, while providing code examples and migration resources.
According to the release schedule, PHP 8 is slated for public release on November 26, with the first Alpha version arriving today after a delay from the originally planned June 18 date.
PHP 8 is a major version that brings significant changes, new features, and performance improvements. Below are some of the most notable highlights.
JIT (Just‑In‑Time) compilation
The most important new feature is JIT, a compiler strategy that translates intermediate code generated by the Zend VM into architecture‑specific machine code at runtime, allowing the CPU to execute the code directly instead of interpreting it.
Since PHP 7.0, performance gains have come from optimising core data structures, enhancing certain opcodes, and improving the OPCache optimizer, but these optimisations are reaching their limits. JIT, by working at a lower level, is considered the best path forward for further performance boosts.
For performance comparisons after JIT’s introduction (and overall PHP 8 performance), see the Phoronix benchmark conducted with the May‑end source build.
Union Types
Union Types allow a parameter or return value to accept multiple distinct types, effectively creating a set of possible types from which one is chosen. This brings type information from phpdoc into function signatures and represents a major breakthrough after generics.
Attributes (Annotations)
Attributes, similar to annotations in other languages, provide a way to attach metadata to classes without parsing docblocks.
New static return type
While PHP already supports self as a return type, the new static return type is more efficient for a dynamically typed language and is useful for many developers.
class Foo {
public function test(): static {
return new static();
}
}WeakMap (Weak Mapping)
WeakMap enables creation of mappings from objects to arbitrary values without preventing the objects used as keys from being garbage‑collected. When an object is added to a WeakMap, the GC can reclaim its memory once it is no longer referenced elsewhere.
PHP 7.4 introduced WeakReference support, but WeakMap is more practical because it does not require registration of destruction callbacks. Typical use cases involve associating data with an object instance while allowing the object to be freed, reducing memory usage in long‑running processes.
class Foo {
private WeakMap $cache;
public function getSomethingWithCaching(object $obj): object {
return $this->cache[$obj] ??= $this->computeSomethingExpensive($obj);
}
}For more details on WeakMap/WeakReference, see the WeakRef section of the linked JavaScript article, as the underlying principles are similar.
Additional resources and related features can be found at:
https://www.php.net/index.php#id2020-06-25-1
https://wiki.php.net/rfc
https://github.com/php/php-src/blob/master/UPGRADING
https://derickrethans.nl/archive.html (Derick Rethans’s blog, core contributor and release manager for PHP 7.4)
Features currently in the voting stage, such as match expressions and proposals to replace <<>> with @@ or #[] , are also worth watching.
Because PHP 8 is a major update with many breaking changes, it is advisable to review the UPGRADING documentation. However, many of the changes were already introduced in earlier 7.x releases, so staying up‑to‑date minimizes migration impact.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.