Felldeadbird
V2EX  ›  PHP

PHP8 大家怎样修复 notice 变成 warning 的错误?

  •  
  •   Felldeadbird · May 9, 2022 · 3085 views
  •   You need to sign in to view this topic
    This topic created in 1556 days ago, the information mentioned may be changed or developed.

    最近打算升级到 PHP8 ,发现程序好多地方报 warning 了。官方搜索了一下,大概就是以前是 notice 升级了。

    A number of notices have been converted into warnings:

    Attempting to read an undefined variable. Attempting to read an undefined property. Attempting to read an undefined array key.

    好多地方要进行变量判断。有点不想干了。v😂

    Supplement 1  ·  May 9, 2022

    太惨了。我代码好多地方用到 $_POST, $_GET。现在要好多地方都要用 $_GET ?? null 。非常不优雅了。

    
     if($_GET['e'] == 1)
    
    
    12 replies    2022-05-18 12:06:06 +08:00
    Rache1
        1
    Rache1  
       May 9, 2022   ❤️ 1
    😄 8.2 还通过了一个新的 RFC ,遗弃了对象的动态属性,还是趁早改代码吧。

    PHP: rfc:deprecate_dynamic_properties
    https://wiki.php.net/rfc/deprecate_dynamic_properties
    Felldeadbird
        2
    Felldeadbird  
    OP
       May 9, 2022
    @Rache1 哎呀,这改动有点激进啊。

    今天一个早上在改这些警告判断。找到一个和我差不多痛苦的人 https://stackoverflow.com/questions/71025115/dealing-with-php-8-1-warning-for-undefined-array-key
    Rache1
        3
    Rache1  
       May 9, 2022
    leo108
        4
    leo108  
       May 9, 2022   ❤️ 1
    phpcs & phpstan 从根源上解君愁
    encro
        5
    encro  
       May 9, 2022
    聪明的你,
    将 warning 错误再禁止呗,
    就如你当初禁止 notice 错误一样。
    lslqtz
        6
    lslqtz  
       May 9, 2022
    你就不能再加一个 isset 么……
    encro
        7
    encro  
       May 9, 2022
    或者,
    你可以学习下正则,
    批量查找替换下。


    replace `if($_GET['e'] == 1)` to `if(isset($_GET['e']) && $_GET['e'] == 1)`

    大概 10 年前,就不用楼主这种写法了。。。,开发模式开启 notice 错误,是标配。
    mrgeneral
        8
    mrgeneral  
       May 9, 2022   ❤️ 1
    所以暂时将你眼睛闭了起来

    error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING

    display_errors = Off
    Felldeadbird
        9
    Felldeadbird  
    OP
       May 9, 2022
    @lslqtz 有一些业务判断,加多一个 isset 太长了。 例如 if($a){} 这样我就可以知道$a 存在就执行里面的方法。


    @encro
    @mrgeneral 禁用 notice 是因为 这个项目写的时候,5.X 时代大部分都这样操作。到了 PHP7 也没太多怎么样变化。最近要准备升级到 PHP8 。。。。
    encro
        10
    encro  
       May 9, 2022
    从 5.3 时代开始,
    我就是没有启用 NOTICE 提示的库不用,
    开发模式必须开启 Notice 错误提示,
    这样能少很多 debug 时间。
    msg7086
        11
    msg7086  
       May 10, 2022 via Android
    我记得我十五年前写的 PHP 代码就已经全用 isset 检测了。是我穿越了吗?
    zengzizhao
        12
    zengzizhao  
       May 18, 2022
    $_GET ?? null
    这样不优雅?

    有一些业务判断,加多一个 isset 太长了
    代码逻辑正确是首要的,加一个 isset 太长了,这也能当理由?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1361 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 17:17 · PVG 01:17 · LAX 10:17 · JFK 13:17
    ♥ Do have faith in what you're doing.