I maintain a plugin. Inside this plugin's composer.json
, extra
>handle
is set to pizza
(not the real name).
In one of the controllers, the following code is executed:
$apiKey = Plugin::$plugin->getSettings()->apiKey;
Prior to upgrading to Craft 4 (specifically, 4.3.1), this worked fine on Craft 3. Now, on Craft 4: Typed property pizza\models\Settings::$apiKey must not be accessed before initialization
.
I have read through this article and as far as I know we follow everything.
There exists a model:
<?php
namespace pizza\models;
use Craft;
use craft\base\Model;
class Settings extends Model
{
public string $apiKey;
}
Which is provided by the plugin class:
protected function createSettingsModel(): ?Model
{
return new Settings();
}
Furthermore, there exists a file config/pizza.php
with the following:
<?php
use craft\helpers\App;
return [
'apiKey' => App::env('PIZZA_API_KEY'),
];
On the admin page example.com/admin/utilities/php-info
it shows that $_ENV['PIZZA_API_KEY']
is set to a value, it is not null or empty.
I'm lost as to why this property is not initialized. The same plugin, same version does not throw this error on another site that is Craft 4 (albeit 4.2.3, so perhaps there has been a change that breaks this?).