Exception 'Error' with message 'Typed static property…
- User
Exception 'Error' with message 'Typed static property app\models\SessionUser::$_purgeExpiredSessions must not be accessed before initialization'
- Assistant
The error message:
code
Exception 'Error' with message 'Typed static property app\models\SessionUser::$_purgeExpiredSessions must not be accessed before initialization'
The error message:
Exception 'Error' with message 'Typed static property app\models\SessionUser::$_purgeExpiredSessions must not be accessed before initialization'
means that you have a typed static property $_purgeExpiredSessions in your SessionUser class that is being accessed before it has been initialized (i.e., assigned a value).
In PHP 7.4 and later, typed properties (including static ones) must be initialized before you read from them. Unlike untyped properties, accessing an uninitialized typed property triggers a fatal error.
For example:
class SessionUser {
private static bool $_purgeExpiredSessions; // Typed static property, but not initialized
public static function doSomething() {
if (self::$_purgeExpiredSessions) { // Access before initialization!
// ...
}
}
}
If $_purgeExpiredSessions isn't assigned before reading it, PHP throws this error.
Assign a default value so it's always initialized.
class SessionUser {
private static bool $_purgeExpiredSessions = false; // default initialization
}
For example, if you cannot assign a default value, make sure you initialize it somewhere prior to reading it:
class SessionUser {
private static bool $_purgeExpiredSessions;
public static function init() {
self::$_purgeExpiredSessions = false; // or true, depending on logic
}
public static function doSomething() {
if (!isset(self::$_purgeExpiredSessions)) {
self::init();
}
if (self::$_purgeExpiredSessions) {
// ...
}
}
}
Note:
isset()doesn't work with typed properties to test for initialization, so better track initialization manually or use a default value.
You can remove the type and use private static $_purgeExpiredSessions; but it removes type safety, so generally not recommended.
If you want, share your SessionUser class code, and I can help provide a concrete fix.
Not yet included in a published release.