Skip to content

36: Rename Deprecated Identifiers

Replace names that PHP 8.6 has started to reserve. Public libraries should review declarations as well as namespace aliases and string-based registrations.

  • PHP 8.6 CLI. Confirm the version with php --version.
  • Review the previous chapter if you’re following the series in order.

Allow about 10 minutes for the walkthrough and exercise.

A runnable deprecated-identifiers.php example.

Run the following from the root of the Code with PHP repository.

Terminal window
php code/php-8-6/36-deprecated-identifiers/deprecated-identifiers.php
deprecated-identifiers.php
<?php
declare(strict_types=1);
const DEFAULT_LANGUAGE = 'en';
final class MessageRules
{
public const MESSAGE_NAMESPACE = 'billing';
}
function isStatus(string $state): bool
{
return in_array($state, ['pending', 'complete'], true);
}
printf("%s.%s state=%s\n", MessageRules::MESSAGE_NAMESPACE, DEFAULT_LANGUAGE,
isStatus('complete') ? 'valid' : 'invalid');
billing.en state=valid

let and is are deprecated in declarations of types, functions and constants and in the corresponding aliases. Also rename _ constants and affected compile-time aliases, class constants called namespace, and functions called readonly. Audit references when renaming a public symbol.

Old declarationExample replacement
A type or alias called letMessageRules
A function called isisStatus()
A constant called _DEFAULT_LANGUAGE
A class constant called namespaceMESSAGE_NAMESPACE
A function called readonlyloadReadOnlySettings()

Search a branch of your code for those exact identifiers and lint the affected files with php -d error_reporting=-1 -l path.php. Update references along with declarations.

Continue with Migrate mbstring Regular Expressions to PCRE.