PHP7 会有强类型

2015-03-17 09:28:15 +08:00
 lsylsy2
经过相当激烈的讨论,扯淡,漫长的拉锯竞争后PHP7终于接受了int float string等scalar variables的type hittings. https://wiki.php.net/rfc/scalar_type_hints_v5 , 不论我多不喜欢strict types,PHP7的几大核心变更总算都已经尘埃落定了,特性冻结期也开始了。

转自鸟哥微博
10397 次点击
所在节点    PHP
30 条回复
invite
2015-03-17 16:16:50 +08:00
然后PHP就不是PHP了,被一帮人搞的越来越 "高大上" 了。
szopen
2015-03-17 17:11:21 +08:00
无语了,这叫类型提示,什么强类型
jookie
2015-03-17 21:03:35 +08:00
只用PHP52
br00k
2015-03-17 21:20:06 +08:00
确定这叫强类型?逗b了
yangff
2015-03-17 22:29:45 +08:00
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
2015-03-18 01:40:23 +08:00
@yangff 《深入PHP 面向对象、模式与实践》作者呼唤了八九年的功能!

JS未来确实会成为最大赢家,但是跟PHP这个功能无关吧。这个功能主要是用在对象方法的强制返回类型,跟JS有啥关系?
xwsoul
2015-03-18 13:47:31 +08:00
我本身是不反对类型声明的, 毕竟没有定义类型的年代, 也注意到了返回类型的统一以及注释中类型的标注.
但是非要在代码头部加个 declare ... 感觉好恶劣啊, 如果使用第三方库 会受到 declare 影响么?
我...我...我还是去看Java吧...
kn007
2015-03-18 16:49:48 +08:00
要这个功能干嘛,,,
xuwenmang
2015-03-20 12:54:25 +08:00
@xwsoul 只是函数返回类型。。。
@kn007 设计模式里提到这个好用,没做过大项目没用过。
jokerzh
2015-04-01 16:55:16 +08:00
终于找到中文翻译了
http://www.lofter.com/tag/php7

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/177429

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX