看到了问题所在。代码试图访问 $config->type 和 $config->value,但 $config 可能是 null(当数据库中找不到对应的配置项时)。
修复方案 方案1:添加空值检查并返回默认值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public static function obtain ($item ): bool |int |array |string { $config = (new Config ())->where ('item' , $item )->first (); if ($config === null ) { return self ::getDefaultValue ($item ); } return match ($config ->type) { 'bool' => (bool ) $config ->value, 'int' => (int ) $config ->value, 'array' => json_decode ($config ->value, true ), default => (string ) $config ->value, }; } private static function getDefaultValue ($item ) { $defaults = [ 'site_name' => 'Default Site Name' , 'enable_feature' => false , 'max_users' => 100 , ]; return $defaults [$item ] ?? null ; }
方案2:使用更简洁的空值检查(PHP 8.0+) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public static function obtain ($item ): bool |int |array |string { $config = (new Config ())->where ('item' , $item )->first (); if ($config === null ) { return null ; } return match ($config ->type) { 'bool' => (bool ) $config ->value, 'int' => (int ) $config ->value, 'array' => json_decode ($config ->value, true ), default => (string ) $config ->value, }; }
方案3:使用 null 安全操作符(PHP 8.0+) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public static function obtain ($item ): bool |int |array |string { $config = (new Config ())->where ('item' , $item )->first (); $type = $config ?->type ?? 'string' ; $value = $config ?->value ?? '' ; return match ($type ) { 'bool' => (bool ) $value , 'int' => (int ) $value , 'array' => json_decode ($value , true ), default => (string ) $value , }; }
方案4:最推荐的完整解决方案 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public static function obtain ($item ): bool |int |array |string { $config = (new Config ())->where ('item' , $item )->first (); if ($config === null ) { error_log ("Configuration item '{$item} ' not found in database" ); return self ::getDefaultValueForItem ($item ); } try { return match ($config ->type) { 'bool' => (bool ) $config ->value, 'int' => (int ) $config ->value, 'array' => json_decode ($config ->value, true ) ?? [], default => (string ) $config ->value, }; } catch (\Exception $e ) { error_log ("Error processing config item '{$item} ': " . $e ->getMessage ()); return self ::getDefaultValueForItem ($item ); } } private static function getDefaultValueForItem ($item ) { $defaultValues = [ 'site_name' => 'My Website' , 'site_url' => '/' , 'enable_register' => true , 'max_users' => 100 , 'theme_config' => [], ]; return $defaultValues [$item ] ?? '' ; }
建议 我推荐使用方案4 ,因为它:
正确处理空值情况
提供有意义的默认值
记录错误日志便于调试
包含异常处理,更加健壮
请根据您的具体需求选择合适的方案,并记得检查数据库中是否缺少某些配置项的数据。
时间 : 2025-09-17T17:38:04
👤 用户 2025/09/17 09:20:58 [error] 71168#71168: *1312 FastCGI sent in stderr: “PHP message: PHP Warning: Attempt to read property “type” on null in /www/wwwroot/yuebin.uk/src/Models/Config.php on line 34; PHP message: PHP Warning: Attempt to read property “value” on null in /www/wwwroot/yuebin.uk/src/Models/Config.php on line 38” while reading response header from upstream, client: 49.66.100.43, server: yuebin.uk, request: “GET / HTTP/1.1”, upstream: “fastcgi://unix:/var/run/php/php8.2-fpm.sock:”, host: “yuebin.uk”, referrer: “https://yuebin.uk/sub/a39b5210d6f007a6/clash “
时间 : 2025-09-17T17:28:02
🤖 DeepSeek (deepseek-chat) 这个错误表明您的 PHP 代码在尝试读取一个 null 对象的属性。让我分析一下问题并提供解决方案:
问题分析 在 /src/Models/Config.php 文件的第 34 和 38 行,代码试图访问一个 null 对象的 type 和 value 属性:
1 2 3 4 5 $something ->type; $something ->value;
解决方案 1. 检查 Config.php 文件的相关代码 找到第 34 和 38 行附近的代码,添加空值检查:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $configValue = $configObject ->type;$configValue = $configObject ->value;if ($configObject !== null ) { $configValue = $configObject ->type; $configValue = $configObject ->value; } else { $configValue = null ; error_log ("Config object is null for key: " . $key ); }
2. 使用 null 安全操作符(PHP 8.0+) 如果您的 PHP 版本是 8.0 或更高,可以使用 null 安全操作符:
1 2 3 $configValue = $configObject ?->type; $configValue = $configObject ?->value;
3. 检查数据源 这个错误通常是由于数据库查询返回了 null,检查获取配置数据的代码:
1 2 3 4 5 6 7 8 $configObject = Config ::where ('key' , $key )->first ();if ($configObject === null ) { return null ; }
4. 临时解决方案 作为临时解决方案,您可以在代码开头添加错误抑制:
1 2 error_reporting (E_ALL & ~E_WARNING);
但这只是隐藏警告,不是根本解决方案。
建议操作步骤
首先备份 当前 Config.php 文件
检查具体代码 ,确定哪个变量可能是 null
添加适当的空值检查
测试修改 确保功能正常
检查数据库 中的配置数据是否完整
如果您需要更具体的帮助,请分享 Config.php 文件中第 30-40 行的代码,我可以提供更精确的修复建议。
时间 : 2025-09-17T17:28:02
👤 用户 public static function obtain($item): bool|int|array|string { $config = (new Config())->where(‘item’, $item)->first();
return match ($config->type) {
'bool' => (bool) $config->value,
'int' => (int) $config->value,
'array' => json_decode($config->value),
default => (string) $config->value,
};
}
时间 : 2025-09-17T17:38:04
🤖 DeepSeek (deepseek-chat) 看到了问题所在。代码试图访问 $config->type 和 $config->value,但 $config 可能是 null(当数据库中找不到对应的配置项时)。
修复方案 方案1:添加空值检查并返回默认值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public static function obtain ($item ): bool |int |array |string { $config = (new Config ())->where ('item' , $item )->first (); if ($config === null ) { return self ::getDefaultValue ($item ); } return match ($config ->type) { 'bool' => (bool ) $config ->value, 'int' => (int ) $config ->value, 'array' => json_decode ($config ->value, true ), default => (string ) $config ->value, }; } private static function getDefaultValue ($item ) { $defaults = [ 'site_name' => 'Default Site Name' , 'enable_feature' => false , 'max_users' => 100 , ]; return $defaults [$item ] ?? null ; }
方案2:使用更简洁的空值检查(PHP 8.0+) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public static function obtain ($item ): bool |int |array |string { $config = (new Config ())->where ('item' , $item )->first (); if ($config === null ) { return null ; } return match ($config ->type) { 'bool' => (bool ) $config ->value, 'int' => (int ) $config ->value, 'array' => json_decode ($config ->value, true ), default => (string ) $config ->value, }; }
方案3:使用 null 安全操作符(PHP 8.0+) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public static function obtain ($item ): bool |int |array |string { $config = (new Config ())->where ('item' , $item )->first (); $type = $config ?->type ?? 'string' ; $value = $config ?->value ?? '' ; return match ($type ) { 'bool' => (bool ) $value , 'int' => (int ) $value , 'array' => json_decode ($value , true ), default => (string ) $value , }; }
方案4:最推荐的完整解决方案 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public static function obtain ($item ): bool |int |array |string { $config = (new Config ())->where ('item' , $item )->first (); if ($config === null ) { error_log ("Configuration item '{$item} ' not found in database" ); return self ::getDefaultValueForItem ($item ); } try { return match ($config ->type) { 'bool' => (bool ) $config ->value, 'int' => (int ) $config ->value, 'array' => json_decode ($config ->value, true ) ?? [], default => (string ) $config ->value, }; } catch (\Exception $e ) { error_log ("Error processing config item '{$item} ': " . $e ->getMessage ()); return self ::getDefaultValueForItem ($item ); } } private static function getDefaultValueForItem ($item ) { $defaultValues = [ 'site_name' => 'My Website' , 'site_url' => '/' , 'enable_register' => true , 'max_users' => 100 , 'theme_config' => [], ]; return $defaultValues [$item ] ?? '' ; }
建议 我推荐使用方案4 ,因为它:
正确处理空值情况
提供有意义的默认值
记录错误日志便于调试
包含异常处理,更加健壮
请根据您的具体需求选择合适的方案,并记得检查数据库中是否缺少某些配置项的数据。
时间 : 2025-09-17T17:38:04
对话统计