+4 votes
51 views
in Web development by (75.9k points)
reopened
PHP 8: everything you need to know about the new update

1 Answer

+5 votes
by (503k points)
edited
 
Best answer

image

PHP 8: everything you need to know about the new update

PHP is one of the easiest programming languages ​​to learn. With a good PHP tutorial and a lot of motivation, in a few hours you can write your first scripts and execute commands. The current version, PHP 7, already brought with it a great speed optimization, which increased its prestige. Version 8.0 will be a very important update for this open source language..

Developer Brent Roose recently posted an announcement with what's new for the new version of PHP. As this update is still in the works, more changes to it may be announced in the coming months until its release.

Index
  1. When will PHP 8 be released?
  2. Will there be complications with the old codes?
  3. New features in PHP 8
    1. JIT compiler
    2. The JSON extension will always be active
    3. Union types
    4. Static return type
    5. Weak maps
    6. Use :: class on objects
    7. Chainable interface
    8. Convert DateTime
    9. fdiv
    10. Type annotations
    11. Type errors
    12. Reclassified engine warnings
    13. Default error reporting level
    14. @ will no longer suppress fatal errors
    15. Order of concatenation operators
    16. Reflection

When will PHP 8 be released?

After a long period of uncertainty, the developers have announced that the PHP 8 release will take place on November 26, 2020. This coincides with the usual 3-year PHP cycle. Also, in December 2019 PHP 7.4 was released and PHP 7.1 was discontinued. However, many web pages written in PHP still use an old version, which will soon be out of support. Keeping the web page code up to date and using the new versions of PHP can bring some benefits. The new versions offer more variety, the performance is greatly increased and the security level is higher..

You can now try PHP 8!

If you are a customer of the IONOS web host, you can now try the beta version of PHP 8. Not yet? Then contract one of our cheap web hosting offers now. In your IONOS customer account, you can switch from the current version of PHP to the beta version of PHP 8. But beware: using the beta version of PHP 8 on production systems is not recommended.

image
Please switch to PHP 8 in your IONOS customer account to test it out.
Done

The first alpha versions of PHP 8 have already been released. However, these are only trial versions and should not be used in online versions of web pages or applications..

Will there be complications with the old codes?

Since PHP 8 is a major version, note that some older code will not be supported. Most of the changes that could present complications were already obsolete in versions 7.2, 7.3 and 7.4.

The latest changes include:

  • The  real  type
  • Magic quotes legacy
  • array_key_exists ()  with objects
  • FILTER_SANITIZE_MAGIC_QUOTES  filter
  • Reflection  export ()  methods
  • mb_strrpos ()  with encoding as 3rd argument
  • implode ()  parameter order mix
  • Unbinding  $ this  from non-static closures
  • hebrevc ()  function
  • convert_cyr_string ()  function
  • money_format ()  function
  • ezmlm_hash ()  function
  • restore_include_path ()  function
  • allow_url_include  ini directive

If you've kept your code up-to-date, you won't have any problems, even with the new update having incompatible changes with previous versions . In this post on GitHub you can find a complete list with all the changes.

Note

Microsoft has announced that Windows will not support PHP 8 and later versions. However, the PHP developers have already communicated their willingness to fix this problem.

New features in PHP 8

The new version of PHP is expected to introduce new features that will expand the capabilities of web developers.

JIT compiler

The biggest novelty will be the JIT compiler , which will improve performance considerably. PHP is not compiled, but is interpreted line by line. The JIT ( Just in Time ) compiler would compile some of the code at runtime , so it would work very similar to a cached version of the code. With this, performance should improve considerably.

This PHP 8 feature has already been tested very successfully by Pedro Escudero. Used a simple script to compare versions 5.3, 7.4 and 8 (with and without JIT). To do this, he ran the script a hundred times in each version and calculated the average time.

The result was the following values:

Version Time in seconds
5.3 0.64574003219604
7.4 0.10253500938416
8 (without JIT) 0.098223924636841
8 (with JIT) 0.053637981414795

Although the difference between version 7.4 and version 8 without JIT is not very big, between version 7.4 and 8 with JIT it is significant. The JIT compiler provides a performance improvement of more than 45% .

The JSON extension will always be active

In earlier versions of the scripting language , the JSON extension could be optionally disabled. However, since JSON is a very important data format, the PHP developers have decided to leave the extension enabled all the time. This should simplify working with PHP.

Union types

The types junction also appear in other languages such as C / C ++, or Haskell typescript. They allow to create unions of two or more types of data, being able to use any of them. In code, this would look like the following:

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

However, there is a limitation. Void cannot be part of a union type , as it does not return any value. Also, nullable unions can be written with | null or ? , as we show you in this example:

  public function foo(Foo|null $foo): void; public function bar(?Bar $bar): void;  

Static return type

Static is a special class name and in the new version it will be a valid return type next to self & parent.

Weak maps

In PHP 7.4 the WeakRefs already appeared, and now with WeakMaps an extension of this function is offered. WeakMaps and WeakRefs can be used to delete objects, only if the cache still references the object's feature classes. This offers resource-saving object management . An example taken from the documentation:

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

Use :: class on objects

In previous versions you had to use get_class () to assign a class to objects. Now you can apply :: class directly to objects. This helps keep the code cleaner.

Chainable interface

Stringable interface implements an interface automatically. Until now, this step had to be done manually. This can be done for all strings or for those where __toString () is implemented. In the code it would read as follows:

  class Foo { public function __toString(): string { return 'foo'; } } function bar(Stringable $stringable) { /* ? */ } bar(new Foo()); bar('abc');  

Convert DateTime

With PHP 8 it will be considerably easier to convert DateTime . The developers have added DateTime :: createFromInterface () and DatetimeImmutable :: createFromInterface (). In this way DateTime and DateTimeImmutable can be converted from one to the other.

fdiv

With fdiv a division by 0 will be possible. Now INF, -ING or NAN is obtained as return value.

Type annotations

The new version will include correct type annotations for all internal functions and methods.

Type errors

Previously, only user-defined functions triggered TypeErrors. Internal functions issued a warning and returned null . With PHP 8, internal functions will also produce TypeErrors.

Reclassified engine warnings

Until now there were many errors that only issued a warning. This will be updated. Here you will find a complete list of new error messages in PHP.

Default error reporting level

Errors that until now were ignored without generating a warning, will be presented as E_ALL, E_NOTICE and E_DEPRECATED. These bugs existed prior to version 8, but were not visibly reported.

@ will no longer suppress fatal errors

This new feature will also aid in bug detection. Instead of suppressing them with the @ operator as was done until now, display_errors = Off should be used on the server.

Order of concatenation operators

This feature was already introduced in PHP 7.4 and will now be fully functional. PHP will now react more intelligently to multiple operators. As an example we show you the following code:

Until now, PHP interpreted

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

as

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

Now PHP 8 will interpret it as

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

Reflection

The signatures will be exchanged. Previously, Reflection appeared as follows:

  ReflectionClass::newInstance($args); ReflectionFunction::invoke($args); ReflectionMethod::invoke($object, $args);  

In PHP 8, it will be written such that:

  ReflectionClass::newInstance(...$args); ReflectionFunction::invoke(...$args); ReflectionMethod::invoke($object, ...$args);  

To use PHP 7 and PHP 8 simultaneously, proceed as follows:

  ReflectionClass::newInstance($arg = null, ...$args); ReflectionFunction::invoke($arg = null, ...$args); ReflectionMethod::invoke($object, $arg = null, ...$args);  

These features of PHP 8 are already confirmed. Still, more news is expected in the months leading up to its launch.


...