List of new features added in PHP 8 update
PHP , one of the programming languages, is a language often used in web development and can be embedded in HTML . PHP is the latest version of PHP at the time of article creation, but PHP 8 is scheduled to be released at the end of 2020, and Brent explains the functions that will be implemented in PHP 8.
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
The union type is a function that can specify multiple types of variables such as 'int' and 'string' that can usually specify only one, and has been implemented in TypeScript until now. PHP is a language that uses dynamic typing , which automatically determines the type of variables, so it works well with this union type. When using the union type, code as follows.
[code] public function foo (Foo | Bar $ input): int | float; [/ code]
Note that void types, which indicate no return value, cannot be included in a union type.
◆ JIT
JIT is an acronym for 'Just in time' and is introduced to improve PHP performance. PHP is an interpreted programming language that reads and converts machine code into executable code each time a program is executed, and discards the converted code when execution is complete. It was disadvantageous in terms of execution speed when compared to a compiler- type language that converts it to standard-level code. 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
◆ 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.
[code] class Foo
{
public function test (): static
{
return new static ();
}
} [/ code]
◆ WeakMap
A feature called weak references was implemented in PHP 7.4. Normally, the garbage collector that automatically releases the memory area cannot release the object if it is referenced, but the object referenced by the weak reference is subject to release by the garbage collector. By declaring a WeakMap that will be implemented in PHP 8 for an object, even if the object is referenced, it can be released by the garbage collector.
[code] class Foo
{
private WeakMap $ cache;
public function getSomethingWithCaching (object $ obj): object
{
return $ this-> cache [$ obj]
?? = $ this-> computeSomethingExpensive ($ obj);
}
} [/ code]
◆ :: class
Starting with PHP 8, you can use :: class as well as get_class () to get the class name of an object.
[code] $ foo = new Foo ();
var_dump ($ foo :: class); [/ code]
◆ 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.
[code] #DatetimeImmutable to DateTime
DateTime :: createFromInterface (DateTimeInterface $ other);
#From DateTime to DatetimeImmutable
DateTimeImmutable :: createFromInterface (DateTimeInterface $ other); [/ code]
◆ fdiv function
The fdiv function, which performs integer division, is newly implemented, and if you try to divide by 0, INF, -INF, and 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, the precedence of concatenating operands has changed. Take the following code as an example:
[code] echo 'sum:'. $ a + $ b; [/ code]
Until PHP 7, it was interpreted as follows ...
[code] echo ('sum:'. $ a) + $ b; [/ code]
From PHP 8, it is interpreted as follows.
[code] echo 'sum:'. ($ a + $ b); [/ code]
Please note that PHP 8 is still under development and new features planned are subject to change.
Related Posts: