Trying to access array offset on value of type null
When migrating a test suite from PHP 7.1 to PHP 7.4, encountering silent failures that suddenly become loud errors is a classic frustration. In this specific scenario, you have 500 functional tests running in Codeception against a Yii2 application. Most pass as expected, but 39 fail immediately upon execution with the message: "Trying to access array offset on value of type null."
The stack trace points to ErrorHandler.php:83 inside the Codeception vendor directory. It is crucial to recognize that this file is merely a safety net; it catches the exception and prints the generic error message, but it does not provide context about which operation triggered the error or what data was involved at that exact moment. The real work happens in SomeFileHereCest.php:72.
The failing code attempts to assign a configuration value to a protected property:
protected $apiPrefix;
public function _before(FunctionalTester $I)
{
$this->apiPrefix = $this->config['backend']['api_prefix']; // Line 72
}
You have confirmed that $this->config['backend']['api_prefix'] is expected to be a string like "v1". However, the error indicates that PHP is trying to treat this value as an array or object when accessing a key, or more likely, the expression evaluates to null before the assignment logic can proceed in a way the previous PHP version tolerated.
The root cause lies in how PHP 7.4 handles type errors compared to 7.1. In PHP 7.1, attempting to access an array offset on a null value often resulted in a Notice: Undefined array key or a silent conversion depending on strictness settings. By default, PHP 7.4 is significantly stricter regarding type coercion and property promotion.
If $this->config itself is null, accessing ['backend'] will throw a TypeError immediately. Similarly, if $this->config['backend'] returns null, accessing ['api_prefix'] on that null value triggers the specific error you are seeing: "Trying to access array offset on value of type null."
In PHP 7.1, this might have been suppressed as a notice or handled implicitly by the test framework's loose typing. In PHP 7.4, this is a hard TypeError. The migration exposed a configuration path that was previously assumed to exist but does not in certain test contexts.
To resolve this without simply adding isset() checks everywhere (which masks deeper architectural issues), you need to inspect how Codeception initializes the $this->config property in your Cest classes.
Check your tests/_support/FunctionalTester.php or the base class definition for Cests. Often, the configuration is loaded from a file like functional.suite.yml. If that suite defines specific settings only for certain environments, and your failing tests run against an environment where those settings are missing, $this->config might be empty or null in those specific instances.
Here is how to verify the state of the config before it fails:
public function _before(FunctionalTester $I)
{
// Debugging step: Log the actual value before assignment
if (is_null($this->config)) {
throw new \Exception("FunctionalTester config is null. Check suite.yml.");
}
if (!isset($this->config['backend'])) {
throw new \Exception("Missing 'backend' key in FunctionalTester config.");
}
$this->apiPrefix = $this->config['backend']['api_prefix'];
}
While adding explicit checks helps, the better long-term fix is to ensure your functional.suite.yml files are robust. They should define a default value for backend.api_prefix even in minimal configurations to prevent null dereferencing.
# functional.suite.yml
actor: FunctionalTester
modules:
enabled:
- Yii2:
part: envs, fixtures, asserts
cleanup: true
config:
# Ensure these always exist to prevent null errors
api_prefix: 'v1'
By defining the configuration at the suite level rather than relying on it being present in every test's scope, you ensure that $this->config always contains the expected structure. This approach aligns with how modern Yii2 and Codeception setups handle environment-specific configurations.
If you are using Laravel as part of your stack or looking for similar configuration management patterns, resources from https://laravelcompany.com can offer insights into managing robust application configuration that prevents such null-reference errors across different environments. The principle remains the same: never trust external data sources without validating their structure first.
The migration to PHP 7.4 forces you to clean up these loose assumptions. What worked in 7.1 was a side effect of lenient type handling, not a feature. Embracing strictness now prevents subtle bugs from manifesting later in production.
