PHP is commonly used for web development across the globe. The scripting langue has stayed relevant for decades and keeps improving with every version. Companies are continuously hiring dedicated PHP developers for web development. To keep up with the pace, the PHP team continuously releases new versions and new features along with bug fixes. PHP 8.1 was a major PHP version that added many significant features like intersection types, enums, fibers, Readonly properties and Never return types. There is a new set of changes and features released on PHP 8.2 which is the latest release. It has made PHP more developer-friendly. Organizations looking forward to hire dedicated or remote PHP developers should ensure that they are updated with the latest happenings. Let us take a look at the new features and changes.
New Features in PHP 8.2
1. New readonly classes
PHP developers can be all excited about the readonly class added in version 8.2. PHP 8.1 already introduced the readonly properties for the classes. This is the next step ahead of making each typed property readonly. Now, a PHP developer can declare the entire class as readonly, saving a lot of time and typing effort. All the properties in the class inherit it. Here is an example of code where we explicitly declare all the properties of the class as readonly:
class MyClass
{
public readonly string $myValue,
public readonly int $myOtherValue
public readonly string $myAnotherValue
public readonly int $myYetAnotherValue
}
After the release of PHP 8.2, you get to write the given lines of code:
readonly class MyClass
{
public string $myValue,
public int $myOtherValue
public string $myAnotherValue
public int $myYetAnotherValue
}
url: PHP: PHP 8.2.0 Release Announcement
2. Disjunctive Normal Form (DNF) Types
With PHP 8.2 come new DNF Types features. This latest update supports the combination of both the types– Union Types (PHP 8.0) and Intersection Types (PHP 8.1). DNF offers a standard way to their combination that can be handled by the parser. Understanding the correct way to write the DNF types is also important. The following declaration can give parse error as its not written in the DNF form.
- A&(B|C)
- A|(B&(C|D))
The correct DNF Types can be :
- A|B|C
- A|B|(C&D)
- (A&B&C)|null
URL: DNF Types - PHP 8.2 • PHP.Watch
3. Null, false, and true as standalone types
We can say that version 8.2 added three new stand-alone types – null, false and true. This may not be entirely correct as we could already use null and false as possible types after PHP.80 release. This release allows them to be used as a standalone type. This means the PHP developers can now make the PHP types system more descriptive with more accurate declarations on return, property, and parameter types. You can hire a PHP developer to reflect the same in your PHP application.
4. Redact parameters in back traces
The production errors are often sent to the service team that keeps track of them. The developers are notified about the errors and the issue fixing starts. In this process, many stack traces containing sensitive information are sent to the third party. PHP 8.2 release allows you to mark the sensitive information containing passwords, environment variables, and other details to safeguard the sensitive information from accidental changes. Organizations can ask developers about this implementation when they hire PHP developers.
5. New ‘mysqli_execute_query’ Function and ‘mysqli::execute_query’ Method
Many PHP developers must have used mysqli_query() function while taking special care of escaping the user values. This difficulty is taken care of in PHP 8.2 where the developers can easily run parameterized MYSQLi queries using the two new additions in the version. mysqli_execute_query($sql, $params) function and the mysqli::execute method together make this implementation possible.
6. Allow constants in Traits
Traits allow PHP developers to reuse the code across different classes. Until now, we could define only methods and properties. The constants could not be defined. The workaround was to define the constants in the composing class. In PHP 8.2, the PHP developers are allowed to define constants in the traits. Here is an example of the usage:
trait Foo {
public const FLAG_1 = 1;
protected const FLAG_2 = 2;
private const FLAG_3 = 2;
public function doFoo(int $flags): void {
if ($flags & self::FLAG_1) {
echo 'Got flag 1';
}
if ($flags & self::FLAG_2) {
echo 'Got flag 2';
}
if ($flags & self::FLAG_3) {
echo 'Got flag 3';
}
}
}
URL: What's New in PHP 8.2 — New Features, Deprecations, & Changes (kinsta.com)
Minor Changes in PHP 8.2
Besides the major features and additions, there are some minor changes. Let us take a look:
1. Removal of support for libmysql from mysqli
Any effort to configure MYSQLi with libmysql will give an error as its support has been removed in PHP 8.2. In case the developer wants to add the functionalities that come with libmysql, then they need to make a feature request and then add it to mysqlnd. Hiring a PHP development service to get all these changes done in your application would be a great idea.
2. Random extension improvement
Until now, the random functionality in PHP relied on Mersenne Twister state which was stored in the PHP global area. It was difficult to reproduce random values. There are many other related issues. The PHP team is taking note of them and very soon hopefully we will have some solution for the issues.
Conclusion
This is a restricted list of changes and features that every PHP developer might be interested in. To conclude, we can say that PHP 8.2 bring many improvements to PHP 8.0 and PHP 8.1. The best way to keep up with these changes is to update the application. Organizations must Hire PHP Developers today to update their applications, understand these changes, and implement them to get the best results.
About Author
Subscribe to Our Newsletter!
Join us to stay updated with our latest blog updates, marketing tips, service tips, trends, news and announcements!