nonfu
V2EX  ›  PHP

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

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

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

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