PHP 8: All the New and In-Depth PHP 8 Features Guide

 


PHP is one of the best programming languages that’s easy to learn. With the PHP 8 release, the level of excitement in the market has increased. It was released on November 26, 2020, and can download from here. The new PHP 8 comes with a lot of new features and performance improvements.

The major update adds a whole bunch of optimizations and powerful features in the language. Get excited to drive through the most exciting changes and build robust applications. Besides breaking the changes, PHP 8 brings a nice set of features like JIT complier, attributes, union types, etc.

Before moving with new features, let’s understand the problems we faced in the old code.

If you’re planning to opt for PHP 8 feature, make sure you should expect old code is no longer compatible. The most complication addressed in versions 7.2, 7.3, and 7.4. But now in the latest PHP 8.0 release includes.

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

If your code updated with a new version of PHP, you won’t face any problems migrating to PHP 8. Even though the new release comes without backward compatibility changes, you won’t face any problem.

Major new features of PHP 8

PHP 8.0 allows named parameter function in addition to traditional positional parameters. The function given below names the part of the public API. It works as non-standardized DocBlock @no-named-arguments and doesn’t provide backwards compatibility.

str_contains(needle: ‘Bar’, haystack: ‘Foobar’);

In here, the attributes declare meta-data for functions, parameters, classes and properties. The attributes here map to PHP class names fetch programmatically with PHP reflection API. The attributes make it easier and performant to declare attributes. It stores them in DocBlock comments and parses the string to infer them.

#[CustomAttribute]

class Foo {

#[AnotherAttribute(42)]

public function bar(): void {}

}

The new syntax used to declare class property from the class constructor with _construct magic method. The constructor PHP 8.0 supports the claiming the visibility like public, private or protected and type. The registered properties add classes with the same visibility and type declared in the constructor. In the below function, the backwards-incompatible feature helps to reduce boilerplate code when declaring value-object classes.

class User {

public function __construct(private string $name) {}

}

PHP Opcache supports JIT which compiles and caches native instructions. There not much noticeable difference in IO-bound web applications that provides a performance boost for CPU-heavy applications. As JIT is still new and comes with bug fixes, it’s late as a day before PHP 8 released. It makes debugging and profiling more challenging by adding an extra layer to it.

# Enabling JIT in php.ini

opcache.enable=1

opcache.jit_buffer_size=100M

opcache.jit=tracing

The union type extends type declarations using return types, class properties, and parameters to declare more than one type. It supports false as its special type with prevalent legacy code without using exceptions.

function parse_value(string|int|float): string|null {}

It works similar to switch blocks, but match blocks provide a type-safe comparison with supporting return value. It supports multiple matching values that guaranteed that at least one branch matched with ensuring all cases accounted for. The switch blocks convert match blocks, requiring backward compatibility, multiple statements, and fall-through functionality that fits the switch statements.

$response = match(‘test’) {

‘test’ => $this->sendTestAlert(),

‘send’ => $this->sendNuclearAlert(),

};

It’s an operator that provides safety method chaining to return a null value. There’s an operator ?-> that shows null operator short-circuits where the rest of the expression encounters a null value. Later it immediately returns null without causing any errors.

return $user->getAddress()?->getCountry()?->isoCode;

It’s a feature that allows to store and associate arbitrary values for object keys without preventing the garbage collector. It is used for clearing the object when it falls out of scope. Weakmap works similar to Splobjectstorage and allows storage of arbitrary values. It doesn’t prevent the object from being garbage collected.

Summary of new PHP 8 released Functions

  • str_starts_with and str_ends_with

It’s a function that helps check if the string starts or ends with an explicit line.

  • str_contains()

It helps in doing a background check of whether the returned value is different from FALSE value and benefits to search needle within the string.

  • get_debug_type

It works similar to the get type function and reverts the information than the latter one.

  • get_resource_id

Using this, one can get a resource Id without any hustle.

  • preg_last_error_msg

The function helps for manually checking errors and messages for a faster check process.

With Winding-up

Here in this blog post, I have covered the most exciting optimization and features added in PHP 8 release. The most awaited feature is the Just in Time compiler were there so much more added to it. If you’re looking for PHP web development or PHP web application development, then explore PHP services and send the experts your inquiry and get a customized quote based on your business needs.

Comments

Popular posts from this blog

What is Microsoft Dataverse? How can we store or build our data in Microsoft Dataverse?

Android 8.0: Here’s Everything You Need To Know

Why Should You Choose Multi-Tenant Architecture for Your SaaS Application?