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

In PHP 8.1 and higher, passing a null value to a type-hinted function parameter that hasn’t been explicitly marked as “nullable” is deprecated. 

This issue typically arises when using legacy extensions on newer PHP environments (such as PHP 8.1, 8.2, or 8.4) where the code is no longer compatible with stricter type-handling.

So, this error generally occurs when you are using the old extension package and newer PHP version, which means when the code is not compatible with the PHP 8.4 – this error arrives.

The Error Scenario

  • Old PHP (Valid): public function __construct(string $name = null)
  • PHP 8.1+ (Deprecated): This throws a deprecation error because $name is type-hinted as a string, yet the default value is null.
  • The Fix: You must prepend a question mark to the type hint:
    public function __construct(?string $name = null)

In Magento 2, constructors frequently have 10–20 injected dependencies. Manually adding the ? symbol across hundreds of files in the app/code directory is inefficient. This script is an Automated Compatibility Patch designed to handle that exact process for you.

You can use this Script, when you are: 

  • Moving from Magento 2.4.3/2.4.4 (PHP 7.4/8.0) to Magento 2.4.6+ (PHP 8.1/8.2+).
  • Using custom modules or third-party extensions that are no longer maintained but are triggering “Deprecated Functionality” logs.

Steps to Fix PHP 8.1 7 & Nullable Constructor Deprecation Error in Magento 2 

Create one file with .sh extension in the root directory and paste the code below:

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 "=============================================="

After adding this script, you will need to run below commands to run this script.

chmod +x fix-php81-nullable.sh 
./fix-php81-nullable.sh

This script is an Automated Compatibility Patch designed specifically for Magento 2 developers upgrading their stores to PHP 8.1 or higher.

After running the script, you must run the following Magento commands:

  1. php bin/magento setup:di:compile
  2. php bin/magento cache:flush:

Upgrading to PHP 8.1+ is essential for Magento security and performance, but the resulting deprecation warnings can be overwhelming. 

By using this automated script, you can instantly resolve nullable constructor errors across all your custom modules, ensuring a clean log and a stable environment with minimal manual effort.

Sanjay Jethva

Article by

Sanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for...