lsylsy2
V2EX  ›  PHP

PHP7 会有强类型

  •  
  •   lsylsy2 · Mar 17, 2015 via Android · 11643 views
    This topic created in 4259 days ago, the information mentioned may be changed or developed.
    经过相当激烈的讨论,扯淡,漫长的拉锯竞争后PHP7终于接受了int float string等scalar variables的type hittings. https://wiki.php.net/rfc/scalar_type_hints_v5 , 不论我多不喜欢strict types,PHP7的几大核心变更总算都已经尘埃落定了,特性冻结期也开始了。

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

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

    说不定啥时候真的写着写着突然有一天发现竟然能用gcc编译了。
    xuwenmang
    xuwenmang
    Mar 17, 2015
    19
    被facebook的hacklang逼的。。。
    justfindu
    justfindu
    Mar 17, 2015
    20
    不复制后面一段么...强类型是不默认开启, 以后也是不默认开启...
    invite
    invite
    Mar 17, 2015
    21
    然后PHP就不是PHP了,被一帮人搞的越来越 "高大上" 了。
    szopen
    szopen
    Mar 17, 2015
    22
    无语了,这叫类型提示,什么强类型
    jookie
    jookie
    Mar 17, 2015
    23
    只用PHP52
    br00k
    br00k
    Mar 17, 2015
    24
    确定这叫强类型?逗b了
    yangff
    yangff
    Mar 17, 2015 via Android
    25
    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
    xuwenmang
    Mar 18, 2015
    26
    @yangff 《深入PHP 面向对象、模式与实践》作者呼唤了八九年的功能!

    JS未来确实会成为最大赢家,但是跟PHP这个功能无关吧。这个功能主要是用在对象方法的强制返回类型,跟JS有啥关系?
    xwsoul
    xwsoul
    Mar 18, 2015
    27
    我本身是不反对类型声明的, 毕竟没有定义类型的年代, 也注意到了返回类型的统一以及注释中类型的标注.
    但是非要在代码头部加个 declare ... 感觉好恶劣啊, 如果使用第三方库 会受到 declare 影响么?
    我...我...我还是去看Java吧...
    kn007
    kn007
    Mar 18, 2015
    28
    要这个功能干嘛,,,
    xuwenmang
    xuwenmang
    Mar 20, 2015
    29
    @xwsoul 只是函数返回类型。。。
    @kn007 设计模式里提到这个好用,没做过大项目没用过。
    jokerzh
    jokerzh
    Apr 1, 2015
    30
    终于找到中文翻译了
    http://www.lofter.com/tag/php7
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2048 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 70ms · UTC 14:53 · PVG 22:53 · LAX 06:53 · JFK 09:53
    ♥ Do have faith in what you're doing.