What's New in PHP 8.1?

What's New in PHP 8.1?

Cover image for What's New in PHP 8.1?

The Dev Drawer

The Dev Drawer

Posted on Mar 17

What's New in PHP 8.1?

#php#webdev#tutorial#productivity

If you are still using PHP 7 then this article is for you. There is some highly-reqested functionality built into PHP 8, specifically PHP 8.1 that you need to know. It will make your programming life much easier.

View This On YouTube

In this article, I am going to show you the PHP 7 way, then I am going to show you the PHP 8 way. Let cut to the chase.

NULLSafe Operators

If you've used the null coalescing operator in the past, you probably also noticed its shortcomings: null coalescing doesn't work on method calls.

The nullsafe operator provides functionality similar to null coalescing, but also supports method calls.

The NULLSafe Operator will allow you to check for nulls on a method so there is no need for an IF statement within the response itself. Instead you would simple add ? after the method to check it for a null return.

PHP 7

classUser{publicfunctiongetProfile(){returnnull;returnnewProfile;}}classProfile{publicfunctiongetTitle(){returnnull;return"Software Engineer";}}$user=newUser;$profile=$user->getProfile();/*
pre null coalescing
if ($profile) {
    if($profile->getTitle()) {
        echo $profile->getTitle();
    }else{
        echo 'Not provided';
    }
}
*//* post null coalescing */echo($profile?($profile->getTitle()??'Not provided'):'Not provided');

Enter fullscreen modeExit fullscreen mode

PHP 8

classUser{publicfunctiongetProfile(){//return null;returnnewProfile;}}classProfile{publicfunctiongetTitle(){//return null;return"Software Engineer";}}$user=newUser;$profile=$user->getProfile();// uses NULL Operator after the getProfile() methodecho$user->getProfile()?->getTitle()??'Not provided';

Enter fullscreen modeExit fullscreen mode

Constructor Property Promotion

Property promotion allows you to combine class fields, constructor definition and variable assignments all into one syntax, in the construct parameter list.

Ditch all the class properties and the variable assignments, and prefix the constructor parameters with public, protected or private. PHP will take that new syntax, and transform it to normal syntax under the hood, before actually executing the code.

NOTE: Promoted properties can only be used in constructors.

PHP 7

classSignup{protectedUserInfo$user;protectedPLanInfo$plan;publicfunction__construct(UserInfo$user,PlanInfo$plan){$this->user=$user;$this->plan=$plan;}}classUserInfo{protectedstring$username;publicfunction__construct($username){$this->username=$username;}}classPlanInfo{protectedstring$name;publicfunction__construct($name='yearly'){$this->name=$name;}}$userInfo=newUserInfo('Test Account');$planInfo=newPlanInfo('monthly');$signup=newSignup($userInfo,$planInfo);

Enter fullscreen modeExit fullscreen mode

PHP 8

classSignup{publicfunction__construct(protectedUserInfo$user,protectedPlanInfo$plan){}}classUserInfo{publicfunction__construct(protectedstring$username){}}classPlanInfo{publicfunction__construct(protectedstring$name='yearly'){}}$userInfo=newUserInfo('Test Account');$planInfo=newPlanInfo('monthly');$signup=newSignup($userInfo,$planInfo);

Enter fullscreen modeExit fullscreen mode

Match Expressions

PHP 8 introduces the new match expression. A powerful feature that will often be the better choice to using switch.

NOTE: Match will do strict type checks instead of loose ones. It's like using === instead of ==.

PHP 7

$test='Send';switch($test){case'Send':$type='send_message';break;case'Remove':$type='remove_message';break;}echo$type;

Enter fullscreen modeExit fullscreen mode

PHP 8

$test='Send';$type=match($test){'Send'=>'send_message','Remove'=>'remove_message'};echo$type;

Enter fullscreen modeExit fullscreen mode

$object::class

It is now possible to fetch the class name of an object using $object::class. The result is the same as get_class($object).

If you working on php long enough I believe you have tried doing class syntax like this Class::method or get_class(new Class()) to fetch a class name as a string. However, if you assign a dynamic class name into a variable and use class syntax like this $object::class you will get an error "Cannot use ::class with dynamic class name". In PHP 8, you can.

PHP 7
N/A, shows error

PHP 8

classSend{}$message=newSend();$type=match($message::class){'Send'=>'send_message','Remove'=>'remove_message'};

Enter fullscreen modeExit fullscreen mode

Named Parameters/Arguments

Named arguments allow you to pass input data into a function, based on their argument name instead of the argument order. This type of functionality is good for keeping tracking of what means what. If you go back to your code and see where you are a calling a method, you may get confused as to what you are calling.

Named parameters help prevent this. They give you the ability to tell your future self what type of variables you are assigning to the method without having to dig through your code and figure out what it does.

NOTE: If your argument names change in the method, it will break your code as the named parameter is no longer valid.

PHP 7

classInvoice{private$customer;private$amount;private$date;publicfunction__construct($customer,$amount,$date){$this->customer=$customer;$this->amount=$amount;$this->date=$date;}}$invoice=newInvoice('Test Account',100,newDateTime);

Enter fullscreen modeExit fullscreen mode

PHP 8

classInvoice{publicfunction__construct(privatestring$customer,privateint$amount,privatedateTime$date){}}$invoice=newInvoice(customer:'Test Account',amount:100,date:newDateTime);

Enter fullscreen modeExit fullscreen mode

String Helpers

Have you ever wanted to find a string in a string in PHP? Most likely you have and most likely you know it is a pain since prior to PHP 8, there was not a lookup function for this purpose even thouh it is one of the most request items for PHP since it was created.

In the past, you need to understand how the math worked and match certain parameters, well, that is now gone and the future of string matching is here.

PHP 7

$string='inv_1234_mid_67890_rec';echo'Starts with inv_: '.(substr_compare($string,'inv_',0,strLen('_inc'))===0?"Yes":"No");echo'Ends with _rec: '.(substr_compare($string,'_rec',-strLen('_rec'))===0?"Yes":"No");echo'Contains _mid_: '.(strpos($string,'_mid_')?"Yes":"No");

Enter fullscreen modeExit fullscreen mode

PHP 8

$string='inv_1234_mid_67890_rec';echo'Starts with inv_: '.(str_starts_with($string,'inv_')?"Yes":"No");echo'Ends with _rec: '.(str_ends_with($string,'_rec')?"Yes":"No");echo'Contains _mid_: '.(str_contains($string,'_mid_')?"Yes":"No");

Enter fullscreen modeExit fullscreen mode

Union and Pseudo Types

In versions prior to PHP 8.0, you could only declare a single type for properties, parameters, and return types. PHP 7.1 and newer versions have nullable types, which means you can declare the type to be null with a type declaration similar to ?string or by using PHPDoc comments.

From PHP 8.0, you can declare more than one type for arguments, return types, and class properties.

PHP 7

classFoo{publicfunctionbar(?Foo$foo){echo'Complete';}}$one=newFoo;$two=newFoo;$two->bar($one);$two->bar(null);// string fails as it is expecting Foo or Null$two->bar('Test');

Enter fullscreen modeExit fullscreen mode

PHP 8

classFoo{publicfunctionbar(Foo|string|null$foo){echo'Complete';}}$one=newFoo;$two=newFoo;// everything works$two->bar($one);$two->bar(null);$two->bar('Test');

Enter fullscreen modeExit fullscreen mode

Conclusion

What is your favorite new feature in PHP 8 and PHP 8.1? Mine is the string helpers and match functions because I already use the older version in almost every project I create in PHP. I hope this helps you make the switch to PHP 8.

Discussion (0)

Subscribe

pic

Upload image

TemplatesEditor guide

PersonalModerator

loading Create template

Templates let you quickly answer FAQs or store snippets for re-use.

SubmitPreviewDismiss

Code of ConductReport abuse

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.

Hide child comments as well

Confirm

For further actions, you may consider blocking this person and/or reporting abuse