Technology

List of new features added in PHP 8 update


Note

List of new features added in PHP 8 update

Is one of the programming languagesPHPIs a language commonly used in web development,HTMLThe feature is that it can be embedded in. PHP is the latest version at the time of article creation, but at the end of 2020PHP 8Is scheduled to be released and features planned to be implemented in PHP 8BrentExplains.

New in PHP 8-stitcher.io
https://stitcher.io/blog/new-in-php-8

GitHub-php / php-src: The PHP Interpreter
https://github.com/php/php-src

◆ Union type
Union typeIs a function that can specify multiple types of variables such as "int" and "string" that can usually specify only one.TypeScriptWas implemented in PHP automatically determines the type of variables in the languageDynamic typingBecause it is a language that adopts, it is compatible with this union type. When using the union type, code as follows.

public function foo(Foo|Bar $input): int|float;

Note thatReturn valueThe void type, which indicates that there is no, cannot be included in a union type.

◆ JIT
JITIs an acronym for "Just in time" and is introduced to improve PHP performance. PHP reads the code every time the program is executed, converts it into executable code, and discards the converted code when execution is completeInterpreterA type-based programming language that converts code such as C or Java into machine-readable code in advance.compilerIt was disadvantageous in terms of execution speed when compared to typed languages. JIT is a technology that saves the converted code and reuses the converted code when there is a similar instruction, thereby saving the trouble of converting the code one by one and increasing the speed.

The video below compares the speed of running PHP 7 without JIT and PHP with JIT in a browser. The movie moves smoothly in PHP with JIT on the right.

PHP 7.0 vs JIT PoC-YouTube

(embed) https://www.youtube.com/watch?v=dWH65pmnsrI (/ embed)

◆ Able to specify static type for return type

Previously, the self type could be used to declare the return value, but PHP 8 will also allow the static type.

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


◆ WeakMap

With PHP 7.4Weak referenceA feature called was implemented. Normally, the memory area is automatically releasedGarbage collectorCannot release an object if it is referenced, but objects referenced by weak references are subject to release by the garbage collector. To be implemented in PHP 8WeakMapCan be declared by the garbage collector even if the object is referenced.

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


◆ :: class

From PHP 8, get_class ():: classCan be used.

$foo = new Foo();

var_dump($foo::class);


◆ Conversion between DateTime and DatetimeImmutable

PHP has a mutable DateTime class that can change the state even after the object is created, and an immutable DatetimeImmutable class that cannot change the state. From PHP 8, conversion can be easily performed as follows.

#DatetimeImmutableからDateTimeへ
DateTime::createFromInterface(DateTimeInterface $other);

#DateTimeからDatetimeImmutableへ
DateTimeImmutable::createFromInterface(DateTimeInterface $other);


◆ fdiv function

A function that performs integer divisionfdiv functionIs implemented, and if you try to divide by 0, INF, -INF, NAN will be returned depending on the situation.

◆ Ensure consistency of TypeError (no backward compatibility)

If the type passed did not match the expected type, the user-defined function returned a TypeError, but the internal function returned null. In PHP 8, both are unified to TypeError.

◆ Arrangement of warnings, notifications, and errors (no backward compatibility)

Organize errors that were previously treated as warnings and notifications as independent errors, and alerts as notifications.

PHP: rfc: engine_warnings
https://wiki.php.net/rfc/engine_warnings

◆ Change of default error level (no backward compatibility)

Errors that were treated as E_NOTICE or E_DEPRECATED until PHP 7 are now collected into E_ALL. This may cause previously ignored errors to appear with updates to PHP 8.

◆ Change of operation of error control operator (no backward compatibility)

Until now, if you described the error control operator @, you could ignore the error and proceed with the process, but from PHP 8, even if you write @, a fatal error will be displayed.

◆ Priority when linking operands (no backward compatibility)

In PHP 8,operandThe priority of concatenating has been changed. Take the following code as an example:

echo "sum: " . $a + $b;

Until PHP 7, it was interpreted as follows …

echo ("sum: " . $a) + $b;

From PHP 8, it is interpreted as follows.

echo "sum: " . ($a + $b);

Please note that PHP 8 is still under development and new features planned are subject to change.

Copy the title and URL of this article

Source link

Do you like this article??

Show More

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button