Konstantinfo

PHP 8 Features, Challenges, Trends in 2021

PHP 8 was officially released on November 20, 2021. PHP 8.0 is the latest version of PHP (updated February 2021). It brings in advanced security, more than PHP 7.4 and older versions. It comes with major issue fixes and performance improvements for WordPress sites.

We’re about to discuss major features, shortcomings and trends that PHP 8 comes up with within 2021.

PHP 8 Features

Improvements: In PHP 8

PHP 8.0 is faster than PHP 7.3/7.4. At most 65% of websites including Facebook, Wikipedia, WordPress support PHP.   It features improvements in –

(1) named arguments,

(2) union types,

(3) constructor property promotion,

(4) constructor,

(5) match expression,

(6) JIT,

(7) null-safe operator,

(8) error handling,

(9) improvements in the type system,

(10) better performance,

(11) consistency,

(12) type safety and better syntax.

(13) A trailing comma in parameter lists RFC and closure use lists RFC,

(14) Non-capturing catches RFC,

(15) Variable Syntax Tweaks RFC,

(16) Treat namespaced names as single token RFC,

(17) Throw is now an expression RFC,

(18) Allow ::class on objects RFC,

(19) New Classes, Interfaces, and Functions – (i) Weak Map class, (ii) Stringable interface, (iii) str_contains(), str_starts_with(), str_ends_with(), (iv) fdiv(),  (v)   get_debug_type(),  (vi) get_resource_id(), (vii) token_get_all() object implementation, (viii)  New DOM Traversal and Manipulation APIs

Prominent Applications: Companies Using PHP

PHP is being used by many famous companies like (1) Facebook, (2) Slack, (3) Wikipedia, (4) Hootsuite, (5) 9GAG, (6) Vimeo, (7) Tesla and (8) WordPress etc.

Technology Stack: Used by Major Companies

Facebook Technology Stack

Hootsuite Technology Stack

9Gag Technology Stack

Backwards Compatibility: Feature Support

If an attribute is used in its line, PHP 8.0 will not cause an error. PHP ignores those attributes. Previous versions of PHP (7.0, 7.1, 7.2, 7.3 and 7.4) feature support for (1) typed properties, (2) support for arrow functions, (3) covariant returns, (4) contravarian parameters, (5) coalescence assignment operator support, (6) support for weak references, (7) preloading, (8) propagation operator in array expression and (9) deprecations.

Trends: PHP Trends in 2021

PHP is and will remain one of the basic building blocks of web development. Coming together with third-party frameworks, PHP accounts for robust and productive applications.

Best PHP frameworks in 2021 are (1) Laravel, (2) Phalcon, (3) Symfony, (4) CodeIgniter, (5) CakePHP, (6) Yii, (7) Slim Framework and (8) Lumen.

PHP works wonders with CMS like WordPress and Drupal.

Developers choose PHP frameworks based on performance, security, advanced features, and the level of community support. If all this in place, it becomes easier to check with upcoming PHP features and build a web application likewise.

Differences PHP 7 vs. PHP 8

Differences Description PHP 7 PHP 8
Named Arguments Arguments, parameters are order-dependent, self-documented.    
Attributes We can now use structured metadata with PHP’s native syntax instead of PHPDoc annotations.    
Constructor Property Less repetitive code sections to define and initialize properties.    
Union Types Use of Native union type declarations instead of PHPDoc annotations for a combination of types validated at runtime.    
Match Expression The result of a match expression is stored in a variable. ‘Match’ (1) Performs strict comparisons,

(2) Supports single-line expressions,

(3) Does not need a break.

   
Nullsafe operator Instead of null check conditions, you can now use a chain of calls with the new null safe operator. When the evaluation of one element in the chain fails, the execution of the entire chain aborts and the entire chain evaluates to null.   }

}

$country = $session?->user?->getAddress()?->country;
String to number comparisons PHP 8 uses a number comparison, while comparing to a numeric string. 0 ==’foobar’ // true  0 ==’foobar’ // false
Consistent type errors for internal functions The internal functions throw an Error exception in case the validation of the parameters fails.    

As we go ahead, exploring the migration from PHP 7.4.x to PHP 8.0.x, the enhancements include:

DateTime::createFromInterface() and DateTimeImmutable::createFromInterface() for DateTime format specifier p – same as P but returns Z rather than +00:00 for UTC.

DOMParentNode and DOMChildNode now come with a new traversal and manipulation API.

FILTER_VALIDATE_BOOLEAN is now known as FILTER_VALIDATE_BOOL.

enchant_dict_add()enchant_dict_is_added(), and LIBENCHANT_VERSION

It comes with pm.status_listen to get status from different endpoints (port or UDS file).

HashContext objects are now serializable.

Some constants are added like

It now has ldap_count_references().

It comes with Cryptographic Message Syntax (CMS) with addition of new encoding constants: OPENSSL_ENCODING_DER, OPENSSL_ENCODING_SMIME and OPENSSL_ENCODING_PEM:

It comes with preg_last_error_msg() and complements preg_last_error() which retruns an integer enum value.

A few methods return default values of parameters/internal functions

SQLite3::setAuthorizer() and respective class constants are added to userland callback.

Following methods come along with some changes:

</>

PHP 8 comes with a tokenizer appended to PHPToken. It provides a uniform and ergonomic representation being memory efficient and quick.

It comes with multiple updates (version 1.19.1)

Backward Incompatible Changes

OCI8

ODBC

OpenSSL

Regular Expressions (Perl-Compatible)

PHP Data Objects 

PDO ODBC

PDO MySQL

PostgreSQL

Phar 

Reflection 

Sockets 

Standard PHP Library (SPL) 

Standard Library

Previously all cases resulted in query and fragment being null.

Sysvsem

Tidy

Tokenizer 

XMLReader

XML-RPC

Zip

Zlib

Windows PHP Test Packs

PHP Core

Enchant

LibXML

PGSQL / PDO PGSQL

Standard Library

Zip

Reflection

ReflectionParameter::getType() and the ReflectionType APIs can be used as ReflectionFunction::isDisabled(), ReflectionParameter::getClass()ReflectionParameter::isArray(), ReflectionParameter::isCallable() are deprecated. 

Challenges with PHP 8.0

Create a backup before testing the new version of the WordPress website with PHP 8. Developers need to take a leap of faith and dive into PHP development with WordPress as it is better-off than previous versions.

Conclusive: Are we ready to embrace changes with PHP 8.0?

We learned that PHP 8.0 brings in major changes to – The JIT compiler; Attributes; Named arguments; Match expressions; Throw expressions (rfc) – Arrow functions, Coalesce Operators, Ternary Operators; Static return type(rfc); Union type(rfc) and Mixed types (rfc); and other interesting features like – Make Sorting Stable(rfc); Constructor Property Promotion(rfc); Zero Safe Operator(rfc); str_contains(rfc); Weak Maps(rfc); Non-capturing Catches(rfc);

Including a word of caution here, developers should consider updating after testing once a stable release is available every year with your WordPress Hosting Provider. PHP progresses naturally. And, before we explore the one at hand, a new version becomes available with updates. Decide the way you wish to proceed with the performance and growth of your business. We endure PHP development services within budget. Hire PHP developers on dedicated or outsourcing models.