How to Fix PHP 8.1+ Nullable Constructor Deprecation Errors in Magento 2 (Automated Script)

Upgrading your Magento store to PHP 8.1 or higher is essential for better performance, security, and long-term support. However, this upgrade often introduces deprecation warnings, especially when working with legacy or third-party extensions.

One of the most common issues developers face is related to nullable constructor parameters.

Let’s break it down and fix it efficiently 👇

⚠️ Understanding the Problem

In PHP versions prior to 8.1, the following code was valid:

				
					public function __construct(string $name = null)
				
			

However, starting from PHP 8.1, this pattern is deprecated.

❌ Why?

Because:

  • The parameter is type-hinted as string
  • But the default value is null

This creates a mismatch, and PHP now enforces stricter type handling.

✅ The Correct Fix

You must explicitly declare the parameter as nullable by adding a ?:

				
					public function __construct(?string $name = null)
				
			

✔ This tells PHP that the parameter can accept either a string or null.

💡 Why This Is a Big Issue in Magento 2

In Magento 2:

  • Constructors often contain 10–20 dependencies
  • Large projects may have hundreds of files
  • Manually updating each file is time-consuming and error-prone

🛠️ Automated Solution (Recommended)

Instead of fixing each file manually, you can automate the process using a shell script and PHP-CS-Fixer.

📌 When Should You Use This Script?

Use this solution if:

  • You are upgrading from Magento 2.4.3 / 2.4.4 → 2.4.6+
  • You are moving from PHP 7.4 / 8.0 → PHP 8.1 / 8.2+
  • You are using custom or outdated third-party modules
  • Your logs are filled with “Deprecated Functionality” warnings

📂 Step-by-Step Implementation

1️⃣ Create a Shell Script

Create a file in your Magento root directory:

				
					fix-php81-nullable.sh
				
			

Paste the following script:

				
					set -e

TARGET_DIR="app/code"

echo "=============================================="
echo " Magento PHP 8.1+ Nullable Constructor Fix"
echo " PHP Version: $(php -r 'echo PHP_VERSION;')"
echo " Target Path: $TARGET_DIR"
echo "=============================================="

if [ ! -d "$TARGET_DIR" ]; then
    echo " Target directory not found: $TARGET_DIR"
    exit 1
fi

if ! command -v composer >/dev/null 2>&1; then
    echo "Composer is not installed"
    exit 1
fi

if [ ! -f "vendor/bin/php-cs-fixer" ]; then
    echo "Installing PHP-CS-Fixer..."
    composer require --dev friendsofphp/php-cs-fixer
else
    echo "✔ PHP-CS-Fixer already installed"
fi

echo "Generating PHP-CS-Fixer config..."

cat << 'EOF' > .php-cs-fixer.php
<?php

use PhpCsFixer\Config;
use PhpCsFixer\Finder;

$finder = Finder::create()
    ->in(__DIR__ . '/app/code')
    ->name('*.php');

return (new Config())
    ->setRiskyAllowed(true)
    ->setRules([
        'nullable_type_declaration_for_default_null_value' => true,
    ])
    ->setFinder($finder);
EOF

if [ -f ".php-cs-fixer.cache" ]; then
    echo "Removing PHP-CS-Fixer cache..."
    rm -f .php-cs-fixer.cache
fi

echo " Running PHP-CS-Fixer..."

vendor/bin/php-cs-fixer fix --allow-risky=yes

echo "=============================================="
echo "Nullable parameters fixed in app/code"
echo "=============================================="
				
			

2️⃣ Make the Script Executable

				
					chmod +x fix-php81-nullable.sh
				
			

3️⃣ Run the Script

				
					./fix-php81-nullable.sh
				
			

🔄 After Running the Script

Execute the following Magento commands:

				
					php bin/magento setup:di:compile
php bin/magento cache:flush
				
			

🎯 Benefits of This Approach

  •  Saves hours of manual work
  •  Fixes all nullable issues automatically
  •  Keeps your codebase PHP 8.1+ compatible
  • Reduces deprecation warnings in logs
  •  Improves overall code quality

🧠 Final Thoughts

Upgrading to PHP 8.1+ is no longer optional—it’s a necessity for modern Magento development. While deprecation warnings can feel overwhelming, automation tools like PHP-CS-Fixer make the transition smooth and efficient.

By using this script, you can quickly modernize your codebase, ensure compatibility, and maintain a clean, stable Magento environment.