nonfu
V2EX  ›  PHP

PHP 8 所有新特性一览和代码示例

  •  
  •   nonfu · Oct 27, 2020 · 5770 views
    This topic created in 2049 days ago, the information mentioned may be changed or developed.

    PHP 8.0 正式版还有一个月就要发布了,是时候在本地编译安装 RC2 版本体验所有新特性了:

    https://xueyuanjun.com/post/22104

    在所有新特性中,注解应该是最有吸引力的了,会给 PHP 开发引入全新的模式。

    39 replies    2020-10-28 16:33:17 +08:00
    yuancoder
        1
    yuancoder  
       Oct 27, 2020
    这玩意有啥用
    mokeyjay
        2
    mokeyjay  
       Oct 27, 2020
    `?->` 实在是太甜了
    vishun
        3
    vishun  
       Oct 27, 2020
    厉害了,注解确实挺方便的。
    wangritian
        4
    wangritian  
       Oct 27, 2020
    一直很喜欢 python 的参数设计,没想到 php 也有了
    nonfu
        5
    nonfu  
    OP
       Oct 27, 2020
    @mokeyjay 感觉一下子就能把代码库的代码量减少 10% 哈哈 再不用 if 判断或者引入空对象模式了
    nonfu
        6
    nonfu  
    OP
       Oct 27, 2020
    @vishun @wangritian 是的 以后日志 中间件之类的业务逻辑实现就更方便了
    hakono
        7
    hakono  
       Oct 27, 2020
    居然已经要发布了,不知道 laravel 啥时候支持,性能会不会有提升
    不过生产环境迁移到 php8 估计还得等个半年一年解决好了兼容性 /BUG 等问题之类吧
    libasten
        8
    libasten  
       Oct 27, 2020 via Android
    不是已经要死亡了吗?咋还发新版本了?
    nonfu
        9
    nonfu  
    OP
       Oct 27, 2020
    @hakono 估计要到明年 9 月份的 LTS 版本会支持,按照现在的节奏,8.x 对应 php 7.3,明年 2 月份的 9.x 对应 php 7.4,有了注解后,至少日志和中间件模块可以重写了。
    nonfu
        10
    nonfu  
    OP
       Oct 27, 2020
    @libasten PHP:我不要面子的啊,好歹还在前十里面,好歹世界上还有接近 80%的网站基于 PHP 驱动
    baobao1270
        11
    baobao1270  
       Oct 27, 2020
    感觉 PHP 越来越像一门现代语言了(?

    遗憾的是,泛型依然没有
    zsdroid
        12
    zsdroid  
       Oct 27, 2020
    注解这套东西还得 ide 的支持
    nonfu
        13
    nonfu  
    OP
       Oct 27, 2020
    @baobao1270 哈哈 动态语言要泛型干啥。。。数组里面随便塞,来者不拒
    eason1874
        14
    eason1874  
       Oct 27, 2020
    PHP7 之前就不怎么写 PHP 代码了,现在不怎么认识 PHP 了
    nonfu
        15
    nonfu  
    OP
       Oct 27, 2020
    @zsdroid phpstorm 2020.3 版本已经支持了
    nonfu
        16
    nonfu  
    OP
       Oct 27, 2020   ❤️ 1
    @eason1874 不会的 最后大家都长得差不多了 动态语言为了安全和性能抄抄静态语言 静态语言为了方便和编码效率抄抄动态语言 然后在语法糖方面不管动态静态相互抄抄 大抵就是这样
    CodeCodeStudy
        17
    CodeCodeStudy  
       Oct 27, 2020
    PHP 9 有什么计划?
    OldActorsSmile
        18
    OldActorsSmile  
       Oct 27, 2020
    一直用框架。。如果不是原生开发,一般不怎么关注版本问题
    polymerdg
        19
    polymerdg  
       Oct 27, 2020
    @mokeyjay 確實有點意思 省去了很多 IF
    lovecy
        20
    lovecy  
       Oct 27, 2020
    很香,然而我司甚至还有 5.2 的代码
    nonfu
        21
    nonfu  
    OP
       Oct 27, 2020
    @OldActorsSmile 没事,Laravel 一直紧随潮流,用 Laravel 最新版也就等同于追着 PHP 版本跑了
    nonfu
        22
    nonfu  
    OP
       Oct 27, 2020
    @CodeCodeStudy PHP 9 估计得四五年以后的事情了
    nonfu
        23
    nonfu  
    OP
       Oct 27, 2020
    @lovecy 。。。连命名空间都没有 Composer 都不能用?
    tabris17
        24
    tabris17  
       Oct 27, 2020
    啥时候能吧变量名前的$去掉就真的脱胎换骨了
    nonfu
        25
    nonfu  
    OP
       Oct 27, 2020
    @tabris17 这个不行 如果不是缺钱 谁去写 PHP 代码 😂
    justseemore
        26
    justseemore  
       Oct 27, 2020
    @tabris17 改成 @么。。
    newtype0092
        27
    newtype0092  
       Oct 27, 2020
    参数列表中允许出现可选的尾部逗号,这个好像 7.4 也是支持的?
    nonfu
        28
    nonfu  
    OP
       Oct 27, 2020
    @newtype0092 不支持啊 rfc 里面该特性目标版本就是 8.0: https://wiki.php.net/rfc/trailing_comma_in_parameter_list
    ben1024
        29
    ben1024  
       Oct 27, 2020
    注解不太喜欢
    akira
        30
    akira  
       Oct 27, 2020
    新增对命名参数的支持 , 终于支持了啊。。
    OldActorsSmile
        31
    OldActorsSmile  
       Oct 27, 2020
    @nonfu

    ThinkPHP 。。让您见笑了
    SaigyoujiYuyuko
        32
    SaigyoujiYuyuko  
       Oct 27, 2020
    冲冲冲
    meshell
        33
    meshell  
       Oct 28, 2020
    @nonfu 7.4 支持的。
    meshell
        34
    meshell  
       Oct 28, 2020
    @nonfu 也可以用
    newtype0092
        35
    newtype0092  
       Oct 28, 2020
    @nonfu 我之前测试环境是 7.1,安装了 Laravel8 最新版,结果运行报错,看了下就是参数列表末尾多了个逗号,我以为是语法错误。后来升级到 7.4 发现可以正常跑了,才知道有这个修改。
    nonfu
        36
    nonfu  
    OP
       Oct 28, 2020
    @meshell 可能是后面迭代的时候加上了
    nonfu
        37
    nonfu  
    OP
       Oct 28, 2020
    @OldActorsSmile 在我这里没有语言框架鄙视链 合适的就是最好的 跑车再牛逼也不敢开到山沟里去
    nonfu
        38
    nonfu  
    OP
       Oct 28, 2020
    @newtype0092 哈哈哈 原来如此 Laravel 8 要求 php7.3+ 莫非 7.3 就支持了?
    binbin0915
        39
    binbin0915  
       Oct 28, 2020
    PHP 宇宙第一.....
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5347 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 96ms · UTC 03:51 · PVG 11:51 · LAX 20:51 · JFK 23:51
    ♥ Do have faith in what you're doing.