Examining Core Deprecations

Learn about the features and functions that have been removed or deprecated in PHP 8.

We'll cover the following

In this lesson, we’ll examine functions and usage that are deprecated in PHP 8. As the PHP language continues to mature, the PHP community is able to suggest to the PHP core development team that certain functions, classes, or even language usage should be removed. If two-thirds of the PHP development team votes in favor of a proposal, it’s adopted for inclusion in a future release of the language.

In the case of functionality to be removed, it is not immediately taken out of the language. Instead, the function, class, method, or usage generates a Deprecation notice. This notice serves as a means to notify developers that this function, class, method, or usage will be disallowed in an as-yet-unspecified release of PHP. Accordingly, we must pay close attention to Deprecation notices. Failure to do so inevitably causes a code break in the future.

Let’s start by examining deprecated usage in parameter order.

Deprecated usage in parameter order

The term “usage” refers to how we call functions and class methods in our application code. We will discover that in PHP 8, older usages were allowed that are now considered bad practices. Understanding how PHP 8 enforces best practices in code usage helps us to write better code.

If we define a function or method with a mixture of mandatory and optional parameters, most PHP developers agree that the optional parameters should follow the mandatory parameters. In PHP 8, this usage best practice, if not followed, will result in a Deprecation notice. The rationale behind the decision to deprecate this usage is to avoid potential logic errors.

This simple example demonstrates this usage difference. In the following example, we define a simple function that accepts three arguments. Note that the $op optional parameter is sandwiched between two mandatory parameters, $a and $b:

Get hands-on with 1200+ tech skills courses.