{"id":25348,"date":"2026-01-13T18:00:00","date_gmt":"2026-01-13T12:30:00","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/?p=25348"},"modified":"2026-01-12T14:21:51","modified_gmt":"2026-01-12T08:51:51","slug":"fix-php-8-1-nullable-constructor-deprecation-errors-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/fix-php-8-1-nullable-constructor-deprecation-errors-magento-2\/","title":{"rendered":"How to Fix PHP 8.1+ Nullable Constructor Deprecation Errors in Magento 2 (Automated Script)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In <strong>PHP 8.1 and higher<\/strong>, passing a null value to a type-hinted function parameter that hasn&#8217;t been explicitly marked as &#8220;nullable&#8221; is deprecated.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n\n\n<p class=\"wp-block-paragraph\">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 &#8211; this error arrives.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Error Scenario<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Old PHP (Valid):<\/strong> public function __construct(string $name = null)<\/li>\n\n\n\n<li><strong>PHP 8.1+ (Deprecated): <\/strong>This throws a deprecation error because $name is type-hinted as a string, yet the default value is null.<\/li>\n\n\n\n<li><strong>The Fix: <\/strong>You must prepend a question mark to the type hint:<br>public function __construct(?string $name = null)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In Magento 2, constructors frequently have 10\u201320 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use this Script, when you are:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Moving from Magento 2.4.3\/2.4.4 (PHP 7.4\/8.0) to Magento 2.4.6+ (PHP 8.1\/8.2+).<\/li>\n\n\n\n<li>Using custom modules or third-party extensions that are no longer maintained but are triggering &#8220;Deprecated Functionality&#8221; logs.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Fix PHP 8.1 7 &amp; Nullable Constructor Deprecation Error in Magento 2&nbsp;<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create one file with .sh extension in the root directory and paste the code below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">set -e\n\nTARGET_DIR=\"app\/code\"\n\necho \"==============================================\"\necho \" Magento PHP 8.1+ Nullable Constructor Fix\"\necho \" PHP Version: $(php -r 'echo PHP_VERSION;')\"\necho \" Target Path: $TARGET_DIR\"\necho \"==============================================\"\n\nif [ ! -d \"$TARGET_DIR\" ]; then\n    echo \" Target directory not found: $TARGET_DIR\"\n    exit 1\nfi\n\nif ! command -v composer >\/dev\/null 2>&amp;1; then\n    echo \"Composer is not installed\"\n    exit 1\nfi\n\nif [ ! -f \"vendor\/bin\/php-cs-fixer\" ]; then\n    echo \"Installing PHP-CS-Fixer...\"\n    composer require --dev friendsofphp\/php-cs-fixer\nelse\n    echo \"\u2714 PHP-CS-Fixer already installed\"\nfi\n\necho \"Generating PHP-CS-Fixer config...\"\n\ncat &lt;&lt; 'EOF' > .php-cs-fixer.php\n&lt;?php\n\nuse PhpCsFixer\\Config;\nuse PhpCsFixer\\Finder;\n\n$finder = Finder::create()\n    ->in(__DIR__ . '\/app\/code')\n    ->name('*.php');\n\nreturn (new Config())\n    ->setRiskyAllowed(true)\n    ->setRules([\n        'nullable_type_declaration_for_default_null_value' => true,\n    ])\n    ->setFinder($finder);\nEOF\n\nif [ -f \".php-cs-fixer.cache\" ]; then\n    echo \"Removing PHP-CS-Fixer cache...\"\n    rm -f .php-cs-fixer.cache\nfi\n\necho \" Running PHP-CS-Fixer...\"\nvendor\/bin\/php-cs-fixer fix --allow-risky=yes\n\necho \"==============================================\"\necho \"Nullable parameters fixed in app\/code\"\necho \"==============================================\"\n\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After adding this script, you will need to run below commands to run this script.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">chmod +x fix-php81-nullable.sh \n.\/fix-php81-nullable.sh\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This script is an Automated Compatibility Patch designed specifically for Magento 2 developers upgrading their stores to PHP 8.1 or higher<strong>.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After running the script, you <strong>must<\/strong> run the following Magento commands:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>php bin\/magento setup:di:compile<\/li>\n\n\n\n<li>php bin\/magento cache:flush:<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Upgrading to PHP 8.1+ is essential for Magento security and performance, but the resulting deprecation warnings can be overwhelming.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PHP 8.1 and higher, passing a null value to a type-hinted function parameter that hasn&#8217;t been explicitly marked as &#8220;nullable&#8221; is deprecated.&nbsp; This issue&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-25348","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/25348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=25348"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/25348\/revisions"}],"predecessor-version":[{"id":25351,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/25348\/revisions\/25351"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=25348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=25348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=25348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}