V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lsylsy2
V2EX  ›  PHP

PHP7 会有强类型

  •  
  •   lsylsy2 · 2015-03-17 09:28:15 +08:00 via Android · 10357 次点击
    这是一个创建于 3321 天前的主题,其中的信息可能已经有所发展或是发生改变。
    经过相当激烈的讨论,扯淡,漫长的拉锯竞争后PHP7终于接受了int float string等scalar variables的type hittings. https://wiki.php.net/rfc/scalar_type_hints_v5 , 不论我多不喜欢strict types,PHP7的几大核心变更总算都已经尘埃落定了,特性冻结期也开始了。

    转自鸟哥微博
    30 条回复    2015-04-01 16:55:16 +08:00
    582033
        1
    582033  
       2015-03-17 09:31:46 +08:00   ❤️ 1
    然后应该跟一句:php依旧是最好的语言..?
    66beta
        2
    66beta  
       2015-03-17 09:33:58 +08:00
    这样下去,PHP8会不会变成Go呀
    kslr
        3
    kslr  
       2015-03-17 09:41:45 +08:00
    支持强类型。
    haiyang416
        4
    haiyang416  
       2015-03-17 09:48:06 +08:00
    declare 又加新参数了
    zkd8907
        5
    zkd8907  
       2015-03-17 09:49:46 +08:00
    已然不是世界上最好的语言
    mahone3297
        6
    mahone3297  
       2015-03-17 09:58:04 +08:00
    支持强类型~
    kisshere
        7
    kisshere  
       2015-03-17 10:14:15 +08:00 via Android
    完了,世界上最好的语言桂冠就这样没了,JavaScript才是世界上最好的语言
    zts1993
        8
    zts1993  
       2015-03-17 10:18:42 +08:00
    这样做还会向前兼容么
    otakustay
        9
    otakustay  
       2015-03-17 10:28:29 +08:00
    ES strawman也准备上强类型了,大势所趋啊,随着系统规模的提升,强类型优势也就慢慢出来了
    anewg
        10
    anewg  
       2015-03-17 10:35:48 +08:00
    @zts1993 默认不开启,以后也不默认开启。
    zhengkai
        11
    zhengkai  
       2015-03-17 10:39:34 +08:00
    如果人多活杂,各种约束是应该有的,PHP 是工业语言,也就不应该是自由的语言。其实就是本来应该编码规范、静态检查该做的事情,强制放到语言特性里

    PHP 从 5.4 就想上常规数据类型的(string、integer)的 type hinting,但一直被推迟,可见开发组对此真的很犹豫。我是支持加 type hinting 的,以我在不同公司的工作经历来看,地球上绝大部分 php 代码都是如此的不堪入目
    mcfog
        12
    mcfog  
       2015-03-17 10:56:15 +08:00
    5.4 5.5都推不开来,度娘上满是php4教程php5.1教程,PHPNG什么的只能先看着馋着了ˊ_>ˋ
    msxcms
        13
    msxcms  
       2015-03-17 11:16:43 +08:00
    不是强制的强类型
    wdlth
        14
    wdlth  
       2015-03-17 11:25:49 +08:00
    这不是变成HACK了么……
    young
        15
    young  
       2015-03-17 12:37:27 +08:00
    都是跑分惹的祸啊
    reusFork
        16
    reusFork  
       2015-03-17 15:23:41 +08:00
    type hinting早就有了,只是之前只能用接口或者trait做,scalar type不能用。
    这只不过让scalar type也能用于hinting而已,算不上什么大事。
    hinting和强类型也是两个概念。
    publicID001
        17
    publicID001  
       2015-03-17 15:54:15 +08:00
    为了无缝转换成C++从而进一步编译为exe吧。
    raincious
        18
    raincious  
       2015-03-17 15:59:36 +08:00
    @publicID001

    说不定啥时候真的写着写着突然有一天发现竟然能用gcc编译了。
    xuwenmang
        19
    xuwenmang  
       2015-03-17 16:02:57 +08:00
    被facebook的hacklang逼的。。。
    justfindu
        20
    justfindu  
       2015-03-17 16:06:53 +08:00
    不复制后面一段么...强类型是不默认开启, 以后也是不默认开启...
    invite
        21
    invite  
       2015-03-17 16:16:50 +08:00
    然后PHP就不是PHP了,被一帮人搞的越来越 "高大上" 了。
    szopen
        22
    szopen  
       2015-03-17 17:11:21 +08:00
    无语了,这叫类型提示,什么强类型
    jookie
        23
    jookie  
       2015-03-17 21:03:35 +08:00
    只用PHP52
    br00k
        24
    br00k  
       2015-03-17 21:20:06 +08:00
    确定这叫强类型?逗b了
    yangff
        25
    yangff  
       2015-03-17 22:29:45 +08:00 via Android
    This RFC further proposes the addition of a new optional per-file directive, declare(strict_types=1);, which makes all function calls and return statements within a file have “strict” type-checking for scalar type declarations, including for extension and built-in PHP functions.

    Balabala......

    然后是...

    Behaviour of strict type checks

    A strictly type-checked call to an extension or built-in PHP function changes the behaviour of zend_parse_parameters. In particular, it will produce E_RECOVERABLE_ERROR rather than E_WARNING on failure, and it follows strict type checking rules for scalar typed parameters, rather than the traditional weak type checking rules.

    The strict type checking rules are quite straightforward: when the type of the value matches that specified by the type declaration it is accepted, otherwise it is not.

    These strict type checking rules are used for userland scalar type declarations, and for extension and built-in PHP functions.

    The one exception is that widening primitive conversion is allowed for int to float. This means that parameters that declare float can also accept int.
    @szopen
    @br00k


    JavaScript或成最大赢家
    xuwenmang
        26
    xuwenmang  
       2015-03-18 01:40:23 +08:00
    @yangff 《深入PHP 面向对象、模式与实践》作者呼唤了八九年的功能!

    JS未来确实会成为最大赢家,但是跟PHP这个功能无关吧。这个功能主要是用在对象方法的强制返回类型,跟JS有啥关系?
    xwsoul
        27
    xwsoul  
       2015-03-18 13:47:31 +08:00
    我本身是不反对类型声明的, 毕竟没有定义类型的年代, 也注意到了返回类型的统一以及注释中类型的标注.
    但是非要在代码头部加个 declare ... 感觉好恶劣啊, 如果使用第三方库 会受到 declare 影响么?
    我...我...我还是去看Java吧...
    kn007
        28
    kn007  
       2015-03-18 16:49:48 +08:00
    要这个功能干嘛,,,
    xuwenmang
        29
    xuwenmang  
       2015-03-20 12:54:25 +08:00
    @xwsoul 只是函数返回类型。。。
    @kn007 设计模式里提到这个好用,没做过大项目没用过。
    jokerzh
        30
    jokerzh  
       2015-04-01 16:55:16 +08:00
    终于找到中文翻译了
    http://www.lofter.com/tag/php7
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1236 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:50 · PVG 01:50 · LAX 10:50 · JFK 13:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.