PHP 8 official GA

The PHP team just announced the  official GA of PHP 8.

Download link: https://www.php.net/downloads

PHP 8.0.0 is the latest major version. It introduces some major changes, as well as many new features and performance improvements. Here are some highlights:

JIT, Just-In-Time, just-in-time compilation

The most important feature is none other than JIT. The research and development of this feature lasted for many years, and survived the PHP 5 and 7 major version series (PHP 6 project aborted), and it was not until March last year that it was finally confirmed by voting to enter PHP 8 .

JIT is a compiler strategy that expresses code as an intermediate state, converts it into machine code that depends on the architecture at runtime, and executes it immediately. In PHP, this means that JIT treats the instructions generated by Zend VM as intermediate representations and executes them in machine code that depends on the architecture, which means that it is no longer Zend VM that hosts the code, but the lower-level CPU .

Although since PHP 7.0, through specific measures such as optimizing the core data structure HashTable, strengthening certain opcodes in Zend VM, and continuously improving OPCache's Optimizer component, PHP performance has been significantly improved, but in fact these optimizations seem to have reached their limits. . Now JIT starts from the bottom and is considered to be the best way to improve PHP performance.

Regarding the performance comparison after the introduction of JIT (and the overall performance of PHP 8), you can refer to the benchmark test of Phoronix (Note: The test was conducted using the source code build version at the end of May). 

Union Types, Union Type

Union Types supports receiving multiple different types of values. It is a collection of two or more types. You can choose one of them when you use it. The use of union types in the standard libraries of open source ecosystems including PHP is very common. PHP supports union types, which will allow more type information to be migrated from phpdoc to function signatures. It can be said that after generics, the joint type is the biggest breakthrough in the current type declaration system.

Attributes, annotation

The Attributes here are the annotations in other languages, which provide a way to add metadata to the class without parsing the document block.

New static return type

Although self can already be returned, considering the nature of PHP's dynamic typing, the support of static return types in PHP 8 will be more efficient and should be very useful for many developers.

class Foo
{
    public function test(): static
    {
        return new static();
    }
}

WeakMap, weak map

WeakMaps allows the creation of mappings from objects to arbitrary values ​​(similar to SplObjectStorage) without preventing garbage collection of objects used as keys. As long as the object is added to the WeakMap, the GC can reclaim the memory it takes up when the condition is triggered.

In PHP 7.4, support for WeakReference (weak reference) has actually been introduced. However, the original weak reference itself is of limited use, and weak mapping is more commonly used in practice. Since there is no function to register and destroy callbacks, it is impossible to implement effective weak mapping on top of weak PHP references. The general use case of weak mapping is to associate data with a single object instance without forcing them to remain active, avoiding long-running processes that use memory unnecessarily.

class Foo 
{
    private WeakMap $cache;
 
    public function getSomethingWithCaching(object $obj): object
    {
        return $this->cache[$obj]
           ??= $this->computeSomethingExpensive($obj);
    }
}

Regarding WeakMap/WeakReference, you can refer to the WeakRef part of this article. Although it is about JS, the principles are the same:

https://www.oschina.net/news/106670/what-s-new-in-javascript-google-io-2019

More related features can be viewed:

  • 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, he is a core PHP contributor and the release manager of PHP 7.4)

It should be noted that PHP 8 is a major update version with some major changes, so it is best to check the related matters of the UPGRADING document. But in fact, many of these major changes have been introduced in the previous 7.* version, so if you always keep the latest version, upgrading to PHP 8 will have little effect.

The main contributor to JIT in PHP 8, @Laruence, has published related articles about the development process of JIT. If you are interested, you can check it out: words written before the release of PHP 8 .

Guess you like

Origin www.oschina.net/news/121754/php-8-ga