Difference between PHP 7 vs PHP 8

Difference between PHP 7 vs PHP 8

The PHP 8 development team brags about increased type safety, better syntax, and greater speed in its announcement. PHP 8 is quicker than PHP 7, and typing is better supported. The syntax also requires less code. An effective major version release of PHP is provided by these three improvement areas.

New Features PHP 8

In November 2020, PHP 8 will be launched, bringing some exciting new features. These additions update a long-running, fruitful effort and bring it into line with more modern languages. These are a few new functions in PHP 8 that you can anticipate.

Name Arguments

Developers can give each provided variable a name with named arguments instead of setting up a temporary variable. Each argument in the example below has a name that provides context. This function removes optional parameters without passing placeholder empty values, such as false or null, or default values in any other way.

function do_the_thing( $one, $two, $three ) { ... }

do_the_thing(
one: 'that got away',
two: 'towers',
three: 'little pigs'
)

the second argument is omitted without passing a default value.

// php7
do_the_thing(
$one = 'that got away',
$two = 'towers,
$three = 'amigos'
);

// php8
do_the_thing(
one: 'that got away',
three: 'amigos'
);


Constructor Property Promotion

Developers' quality of life is improved by constructor property promotion, which minimizes the amount of code required to generate an object with straightforward constructor properties.

The class in the example below receives three constructor arguments that are linked to their respective properties. Keep in mind that the property types are mentioned twice—once in the constructor and once in the property declaration. They fulfill two distinct, yet remarkably similar, functions that demand duplication.

<?php

class DoTheThingAction
{

protected OneCount $one;
protected TwoCount $two;
protected ThreeCount $three;

public function __construct(OneCount $one, TwoCount $two, ThreeCount $three)
{
$this->one = $one;
$this->two = $two;
$this->three = $three;
}
}

This duplication can be eliminated by simply assigning constructor parameters to properties with the same names that correspond thanks to constructor property promotion. Consider the following example, but without the duplication from the previous one: the identical class properties are declared and given their appropriate values via the constructor.

class DoTheThingAction {

public function __construct(
protected OneCount $one,
protected TwoCount $two,
protected ThreeCount $three,
) {
// Nothing else to do here.
}
}


Union Types

Union types benefit from type hinting and add flexibility to variable types. Without union types, a docblock comment would be used in place of the type declaration whenever a variable could only have one of two types, such as NULL or integer. The developer and documentation are informed by the docblock that several types are supported but do not gain from type hinting.

<?php


// Argument with a single type uses a typehint.
function do_the_thing( ThingOne $thing ) { ... }

/**
* Argument supporting multiple types looses typehinting.
* @param ThingOne|ThingTwo $thing
*/
function do_the_thing( $thing ) { ... }

// With untion types, arguments supporting multiple types
// can benefit from typehinting both possible types.
function do_the_thing( ThingOne|ThingTwo $thing ) { ... }

Match Expression

Switch statements and match expressions are similar yet sometimes match expressions work better. Think about the scenario when you create an object whose type is determined by a string. We evaluate the string and add the suitable object to a temporary variable using a switch statement.

<?php


switch( $type ) {
case 'one':
$object = new ThingOne;
break;
case 'two':
$object = new ThingTwo;
break;
case 'three':
$object = new ThingThree;
break;
}

We can accomplish this dynamic assignment without the boilerplate of a switch statement thanks to the match expression. There is no longer a break between each case, and the object variable is not allocated numerous times.

<?php


$object = match( $type ) {
'one' => new ThingOne,
'two' => new ThingTwo,
'three' => new ThingThree,
}

NullSafe Operator

Another quality-of-life enhancement in PHP8 is the nullsafe operator. As seen in the following example, it is no longer required to check for null values every time you visit a property that is numerous levels deep.

<?php

if( null !== $user ) {
if( null !== $user->profile ) {
echo $user->profile->getAvatar();
}
}

Using the nullsafe operator, on the other hand, has less overhead. Before calling the following property, each property down the line is securely verified for null.

<?php

echo $user?->profile?->getAvater();

New PHP 8 Function

With the introduction of new functions and a Stringable interface in PHP 8, strings also received some much-needed attention. With the introduction of consistency between projects and a decrease in logical overhead, these new functions will substitute some custom but frequently used functionality with first-party functions.

String Begins With, String Ends With, and String Includes

The new string methods offer a sensible technique of handling substrings without depending on the string's position. Here are a few illustrations of the many levels of intricacy.

// php7 - Check that the string has a position
// and account for a position of 0
false !== strpos( 'thing-one', 'one');

// php8
str_contains( 'thing-one', 'one' );
<?php

$haystack = 'thing-one';
$needle = 'thing';

// php7
0 === strpos( $haystack, $needle );

// php8
str_starts_with( $haystack, $needle );
<?php

$haystack = 'thing-one';
$needle = 'one';

// php7
strpos( $haystack, 'one') == ( strlen( $haystack ) - strlen( $needle ) );

// php8
str_ends_with( $haystack, $needle );

PHP 8 Performance compared. PHP 7

Continuing the steady advancements made with each minor version of PHP 7, PHP 8 brings another improvement in performance for PHP. There is a gradual improvement in performance with each iteration, even though it is not as noticeable as the difference between PHP 5 and PHP 7.

Final Reflections

Developers will appreciate PHP 8's changes, which are available right out of the box. Users notice performance and speed variations between PHP 7 and PHP 8 in addition to the new features and functions. If you still need to upgrade your Windows or Linux test machines to PHP 8, it could be worthwhile to give it a shot.