You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP-Nuke Titanium中abget_configs函数false转数组废弃警告问题

PHP 8 废弃警告:自动将false转换为数组已被废弃

Deprecated: 自动将false转换为数组已被废弃

我是PHP 8新手,看到的所有相关解释都是针对会话的,而非从数据库获取的数据。
该函数写入缓存正常,Deprecated警告会消失。每次清除缓存时我会看到一次警告,之后就不再出现。一旦开始从缓存文件读取,就再也看不到该警告了。
它会保存缓存文件,然后系统正常使用,就像什么都没发生过一样。我不理解此场景下“自动将false转换为数组”的实际含义。

问题涉及的代码行:

$sentinel[$config_name] = $config_value;

还有什么其他写法可以实现该功能?


原函数代码

function abget_configs(){ 
      global $prefix, $db, $cache;
      static $sentinel;
      
      if(isset($sentinel)) return $sentinel;
      
      if(!($sentinel = $cache->load('titanium_sentinel', 'config'))) {
          $configresult = $db->sql_query("SELECT `config_name`, `config_value` FROM `".$prefix."_nsnst_config`");
          
          while (list($config_name, $config_value) = $db->sql_fetchrow($configresult)) {
            $sentinel[$config_name] = $config_value;
            echo $config_name.': '.$sentinel[$config_name].'</br>';
          }
          $db->sql_freeresult($configresult);
          $cache->save('titanium_sentinel', 'config', $sentinel);
      }
      return $sentinel;
    }

输出结果

admin_contact: admin@php-nuke.coders.exchange
block_perpage: 50
block_sort_column: date
block_sort_direction: desc
crypt_salt: N$
display_link: 3
display_reason: 3
force_nukeurl: 0
help_switch: 1
htaccess_path: /home/phpnuke/public_html/.htaccess
http_auth: 0
lookup_link: admin.php?op=ABIpCheck&domain=
page_delay: 1
prevent_dos: 1
proxy_reason: abuse_admin.tpl
proxy_switch: 0
santy_protection: 1
self_expire: 0
site_reason: admin_site_reason.tpl
site_switch: 0
staccess_path: /home/phpnuke/public_html/.staccess
track_active: 1
track_max: 604800
track_perpage: 50
track_sort_column: ip_long
track_sort_direction: desc
ip_reason: admin_ip_reason.tpl
ip_switch: 0
ftaccess_path: /home/phpnuke/public_html/.ftaccess
flood_delay: 2
disable_switch: 0
track_clear: 1673827200
blocked_clear: 0
version_check: $checktime
version_newest: 2.6.09
version_number: 2.6.09
dump_directory: includes/cache/
show_right: 0
test_switch: 0

var_dump($sentinel) 结果

array(39) { ["admin_contact"]=> string(30) "admin@php-nuke.coders.exchange" ["block_perpage"]=> string(2) "50" ["block_sort_column"]=> string(4) "date" ["block_sort_direction"]=> string(4) "desc" ["crypt_salt"]=> string(2) "N$" ["display_link"]=> string(1) "3" ["display_reason"]=> string(1) "3" ["force_nukeurl"]=> string(1) "0" ["help_switch"]=> string(1) "1" ["htaccess_path"]=> string(35) "/home/phpnuke/public_html/.htaccess" ["http_auth"]=> string(1) "0" ["lookup_link"]=> string(30) "admin.php?op=ABIpCheck&domain=" ["page_delay"]=> string(1) "1" ["prevent_dos"]=> string(1) "1" ["proxy_reason"]=> string(15) "abuse_admin.tpl" ["proxy_switch"]=> string(1) "0" ["santy_protection"]=> string(1) "1" ["self_expire"]=> string(1) "0" ["site_reason"]=> string(21) "admin_site_reason.tpl" ["site_switch"]=> string(1) "0" ["staccess_path"]=> string(35) "/home/phpnuke/public_html/.staccess" ["track_active"]=> string(1) "1" ["track_max"]=> string(6) "604800" ["track_perpage"]=> string(2) "50" ["track_sort_column"]=> string(7) "ip_long" ["track_sort_direction"]=> string(4) "desc" ["ip_reason"]=> string(19) "admin_ip_reason.tpl" ["ip_switch"]=> string(1) "0" ["ftaccess_path"]=> string(35) "/home/phpnuke/public_html/.ftaccess" ["flood_delay"]=> string(1) "2" ["disable_switch"]=> string(1) "0" ["track_clear"]=> string(10) "1673827200" ["blocked_clear"]=> string(1) "0" ["version_check"]=> string(10) "$checktime" ["version_newest"]=> string(6) "2.6.09" ["version_number"]=> string(6) "2.6.09" ["dump_directory"]=> string(15) "includes/cache/" ["show_right"]=> string(1) "0" ["test_switch"]=> string(1) "0" } 

问题原因与修复方法

问题原因

当缓存不存在时,$cache->load()返回false,此时$sentinel被赋值为false。接下来执行$sentinel[$config_name] = $config_value;时,PHP 8会尝试把false自动转为数组来执行赋值操作,这在PHP 8中已被标记为废弃行为,所以抛出警告。

修复写法

在进入数据库查询逻辑前,先将sentinel初始化为空数组,避免将false当作数组操作:

function abget_configs(){ 
      global $prefix, $db, $cache;
      static $sentinel;
      
      if(isset($sentinel)) return $sentinel;
      
      if(!($sentinel = $cache->load('titanium_sentinel', 'config'))) {
          // 初始化空数组,避免false转数组的废弃行为
          $sentinel = [];
          $configresult = $db->sql_query("SELECT `config_name`, `config_value` FROM `".$prefix."_nsnst_config`");
          
          while (list($config_name, $config_value) = $db->sql_fetchrow($configresult)) {
            $sentinel[$config_name] = $config_value;
            echo $config_name.': '.$sentinel[$config_name].'</br>';
          }
          $db->sql_freeresult($configresult);
          $cache->save('titanium_sentinel', 'config', $sentinel);
      }
      return $sentinel;
    }

这样修改后,$sentinel在缓存未命中时会被明确初始化为数组,再执行赋值操作就不会触发废弃警告了。

内容的提问来源于stack exchange,提问作者Ernest Allen Buffington

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 07:15:31