42: Drop the Third Argument to define()
Overview
Section titled “Overview”Register a constant with its canonical spelling. PHP 8.6 deprecates passing the old case-insensitivity argument, even when it is false.
Prerequisites
Section titled “Prerequisites”- 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.
What You’ll Build
Section titled “What You’ll Build”A runnable define-third-argument.php example.
Step 1: Run the example (~5 min)
Section titled “Step 1: Run the example (~5 min)”Run the following from the root of the Code with PHP repository.
php code/php-8-6/42-define-third-argument/define-third-argument.php<?php
declare(strict_types=1);
define('DEFAULT_CURRENCY', 'AUD');echo constant('DEFAULT_CURRENCY'), PHP_EOL;echo 'Lowercase alias exists: ', defined('default_currency') ? 'yes' : 'no', PHP_EOL;Expected Result
Section titled “Expected Result”AUDLowercase alias exists: noWhy It Works
Section titled “Why It Works”Use two arguments and read the constant with its declared casing. If your old code passed true, audit consumers for case mismatches as part of the migration.
Exercise
Section titled “Exercise”Check defined('default_currency') after the declaration. Then check defined('DEFAULT_CURRENCY') and compare the results.
Wrap-up
Section titled “Wrap-up”Continue with Handle Class Names Explicitly in Type Checks.