Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 3, 2020 10:15 pm GMT

Why PHP 8 will be awesome

PHP 8 will be available very soon. It's a major version with breaking changes, but also with excellent features.

Disclaimer

It's a personal selection, not an exhausting list.

Small details but great features

Again, it's not an exhaustive list, but some changes will be handy for our daily PHP routines.

::class available for objects

Instead of calling an intermediary get_class() on objects, you will be able to do that directly:

$myObject = new MyClass();var_dump($myObject::class);
Enter fullscreen mode Exit fullscreen mode

Source

Trailing commas for parameters

In PHP 7, you can add trailing commas in arrays like that:

$myArray = [ "pull", "push", "jump",];
Enter fullscreen mode Exit fullscreen mode

But you cannot do this with function and method parameters. In PHP8, you can!

new Uri(    $scheme,    $user,    $pass,    $host,    $port,    $path,    $query,    $fragment, // <-- Huh, this is allowed!);
Enter fullscreen mode Exit fullscreen mode

Source

str_* new magic

I read about str_contains, str_starts_with() or str_ends_with() in rfc. Simpler, better, stronger than strpos or any regular expression.

Break things for the win

PHP 8 will throw many more errors by default than PHP 7. Everything deprecated before PHP 8 has been removed!

The @ operator

This dark lord of PHP operators will no longer silence fatal errors!!!

Prepare for battle

Fatal error for static calls of non-static methods

The following code already triggers some errors, but in PHP 8, it will raise a fatal error!

class MyClass {    public function myMethod() {}}MyClass::myMethod();
Enter fullscreen mode Exit fullscreen mode

New default error reporting level

E_ALL will be the default value. Again, no more silenced errors.

Don't mess with undefined!

PHP 8 will convert many warnings and notices into errors, for example:

  • undefined variables
  • division by zero

The JIT disclaimer

It's not possible to write any post about PHP 8 without celebrating the JIT birth \0/.

The Just In Time compilation should improve performances, somehow. Before PHP 8, PHP executes code by converting PHP code into instructions (~ opcodes) that the virtual machine runs.

The JIT compiler converts PHP code into x86 machine code, which runs directly on the CPU.

However, as Nikita Popov said, it will be significant for code that involves mathematical calculations, not the average code.

Do not expect the same wow effect as with migrations from PHP 5 to PHP 7.

Killer features

Some features will be decisive.

Union types

Before PHP 8, union types were possible only in phpdoc:

/*** @var int|float $number*/private $number;
Enter fullscreen mode Exit fullscreen mode

With PHP 8, you will write:

private int|float $number
Enter fullscreen mode Exit fullscreen mode

The idea is to move more type information from phpdoc into function signatures. Void won't be available in union types, though, but it would not make sense anyway.

Source

static return type

It will follow the same purpose as the static keyword. Your method should return something from the current (~ child) class, not the parent class.

class MyClass {     public function myMethod(): static {         return new static();     } }
Enter fullscreen mode Exit fullscreen mode

Awesome!

Throw as expression

Moving Throw from statement to expression will allow the following usage in PHP 8:

$condition || throw new Exception();
Enter fullscreen mode Exit fullscreen mode

Source

Love the idea of using Throw in arrow functions or ternaries.

Conclusion

PHP 8 is better, faster, stronger. Can't wait to use it. It looks like the first step toward making PHP a type-safe programming language.


Original Link: https://dev.to/jmau111/why-php-8-will-be-awesome-2lif

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To