Fix PHP 8.1 Nullable Constructor Deprecation Errors in Magento 2
When upgrading Magento 2 to PHP 8.1, many developers encounter PHP 8.1 nullable constructor errors caused by deprecated type declarations.
These warnings may not break your store immediately, but they pollute logs, affect performance, and indicate incompatible code that should be fixed as soon as possible.
In this guide, you’ll learn why PHP 8.1 triggers these deprecation errors in Magento 2, how to fix them properly, and how to bulk-fix custom modules safely.
Why PHP 8.1 Nullable Constructor Errors Occur
Starting from PHP 8.1, passing null to a parameter that is not explicitly declared as nullable is deprecated.
Example of deprecated code in Magento 2
public function __construct(
LoggerInterface $logger = null
) {
$this->logger = $logger;
}
This looks harmless, but in PHP 8.1 it triggers:
Passing null to parameter of type LoggerInterface is deprecated
Why does this happen?
Because:
- The parameter type is not nullable
- But the default value is
null
PHP 8.1 enforces strict type declarations, especially in constructors used for dependency injection — something Magento relies on heavily.
The Correct Way to Fix Nullable Constructor Parameters
To fix the issue, you must explicitly mark the parameter as nullable using the ? operator.
✅ Correct PHP 8.1 compatible code
public function __construct(
?LoggerInterface $logger = null
) {
$this->logger = $logger;
}
This tells PHP:
- The parameter can accept null
- The code is future-proof and type-safe
Where These Errors Commonly Appear in Magento 2
You’ll usually see these warnings in:
- Custom modules inside
app/code - Old third-party extensions
- Overridden core classes (bad practice, but common)
- Legacy code written for PHP 7.2 – 7.4
Magento core (2.4.6+) already supports PHP 8.1, so core files rarely cause this issue.
How to Find Nullable Constructor Issues in Magento 2
Method 1: Check Logs
Look for warnings like:
Deprecated: Passing null to parameter #1 ($logger) of type LoggerInterface is deprecated
Common log locations:
var/log/system.logvar/log/exception.log- PHP error log
Method 2: Scan Code Manually (Small Projects)
Search for patterns like:
$type $variable = null
without a leading ?.
Bulk Fix Nullable Constructor Errors (Recommended)
If your Magento project has many custom modules, fixing files manually is risky and time-consuming.
A safer approach is to automatically detect and fix nullable parameters.
Suggested Workflow
1. Backup your codebase
2. Scan only:
app/code
3. Update constructor parameters to:
?Type $param = null
4. Run static analysis & tests
You can use tools like:
- PHP-CS-Fixer
- Rector
- Custom regex-based scripts (advanced users only)
| ⚠️ Do NOT auto-fix vendor modules unless you know exactly what you’re doing. |
Important Notes Before Applying Fixes
- ❌ Do not blindly add
?to every parameter
- ✅ Only mark parameters nullable if
nullis actually valid
- ✅ Test dependency injection compilation:
php bin/magento setup:di:compile
- ✅ Clear cache after changes:
php bin/magento cache:flush
Magento 2 & PHP 8.1 Compatibility Checklist
Before deploying to production:
- Magento version ≥ 2.4.6
- All custom modules PHP 8.1 compatible
- No deprecated warnings in logs
- DI compilation passes
- Storefront and admin tested
Final Thoughts
PHP 8.1 nullable constructor deprecation errors in Magento 2 are warnings, not fatal errors — but ignoring them is a bad idea.
Fixing them early will:
- Keep logs clean
- Improve long-term maintainability
- Prepare your store for PHP 8.2+
If you’re upgrading Magento environments regularly, handling these deprecations should be part of your standard upgrade checklist.
As you continue improving your Magento 2 store’s backend performance and PHP environment, be sure to explore other articles on this blog that cover related topics in detail. For example, optimizing PHP memory and PHP-FPM settings can greatly enhance performance for Magento 2:
- Learn how to optimize PHP memory usage in Magento 2 for better performance and stability.
- Optimize PHP-FPM for Magento 2 on Nginx if you’re running your store on an Nginx server.
- Configure PHP-FPM with Nginx for improved processing efficiency.
- Install multiple PHP versions on Ubuntu to test compatibility across environments.
These resources can help you maintain a robust Magento 2 environment while addressing PHP 8.1 issues like nullable constructor deprecation errors.