PHP 8.0 – What is new in the major update?

PHP-8.0

PHP (Hypertext Preprocessor) is a general-purpose server scripting language (embedded in HTML) suitable for web development. PHP originally was called Personal Home Page, but now has a recursive initialization Hypertext Preprocessor.

PHP powers everything, from simple blogs to popular websites. It is fast and flexible, making it the best match to build interactive and dynamic web pages. It is a FREE, efficient, and widely used alternative to competitors like Microsoft’s ASP.

In 2015, the new PHP 7 version was developed, which had four more sub-upgrades till PHP 7.4. Overcoming the flaws and bringing many long-awaited features recently, PHP has released the eleventh testing release of PHP 8.0, including some great varieties of effective changes and lean language improvements.

As PHP 8.0 is here, several changes can be seen in terms of performance, features, etc. The most discussed feature of PHP 8.0 is PHP JIT (Just in Time Compiler). JIT is set to increase the speed of an application by the technique used to handle the compilation of the scripts.

The upgrade to PHP 8.0 shouldn’t be that hard if you have kept your code up to date with the latest releases as most changes were deprecated before in the PHP 7.* versions.

The new PHP 8.0 also brings in some more exciting features such as Named Arguments, Attributes, Constructor property promotion, Union Types, Match expression, Just-In-Time compilation and more.

Why Do You Need PHP 8.0?

The latest benchmark tests by Phoronix show PHP 8.0 is performing 10% better than its predecessors. This data suggests a promising future for PHP-based websites and applications.

Better yet, the test was not even conducted using the JIT compiler, a new optimization feature introduced with PHP 8.0. Developers can expect a much faster performance if it is enabled.
Furthermore, the version implements new features to make coding much quicker and cleaner, reducing plenty of boilerplate and redundant code.
Since this is a version update, your website will likely experience changes that might break it if you transition to PHP 8.0 without making any prior modifications.

Major new features of PHP 8

Named Parameters

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’);

Attributes

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.

[Custom Attribute]

class Foo {
#[Another Attribute(42)]
Public function bar (): void {}
}

Constructor Properties

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) {}
}

Just-in-time Compilation

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
Union Types

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 {}

Match Expressions

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(),
};

Null-Safe Operators

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->get Address()?->get Country()?->isoCode;

Weak Maps

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. Weak map works similar to Spl object storage and allows storage of arbitrary values. It doesn’t prevent the object from being garbage collected.

The example below, sourced from the RFC, which shows how weak maps will look like:

$map = new WeakMap;
$obj = new stdClass;
$map[$obj] = 42;
var_dump($map);
// object(WeakMap)#1 (1) {
// [0]=>
// array(2) {
// [“key”]=>
// object(stdClass)#2 (0) {
// }
// [“value”]=>
// int(42)
// }
// }
// The object is destroyed here, and the key is automatically removed from the weak map.
unset($obj);
var_dump($map);
// object(WeakMap)#1 (0) {
// }

This feature will be more beneficial for long-running applications, particularly if you want to prevent memory leaks and implement caching.

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.

Further improvements

Here is the list of some additional approved but still in draft improvements of PHP 8.0:
Stringable interface: It is automatically added to classes implementing _to_string () method. The actual goal here is to use the stringable union type.
Static return type: It is the usage of static as return type next to self and parent type.
• New living standard APIs in ext/dom: It proposes to implement the current DOM living standard to the PHP DOM extension by introducing public properties and interfaces.
• Variable Syntax tweaks: It resolves some residual inconsistencies in PHP’s variable syntax.

With Winding-up

Here in this blog post, we 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 our services. Send us your inquiry and get a customized quote based on your business needs.

Aslo Read –

Explore The Future Of Programming With AI>>

How To Create A SQL Query Using AI?>>

Meta’s New AI Language Model – LLaMA>>

I am Vaibhav Singhal, a full-stack developer with over 12 years of experience in the technical field. My passion for developing and designing web and mobile applications. As a full-stack developer, I have a broad range of technical skills, including proficiency in various programming languages, databases, and development frameworks. I enjoy the challenge of taking a concept or idea and turning it into a fully functional and visually appealing application that meets the needs of the end-users. I am always looking for opportunities to expand my skillset and stay up-to-date with the latest industry trends and technologies.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top