函数类型提示(Callable typehint)
2011-12-02 16:09
495 查看
原文地址: http://www.laruence.com/2011/10/10/2229.html
在PHP5.4的开发过程中, 有不少对typehint的提议, 我们也讨论过很多次, 完全加入对基本类型的typehint会引起很大的变动, 并且就我自己来看, 不利于PHP的灵活性.
不过, 对于callable类型的支持, 我还是支持的.
在以前, 我们如果希望一个函数接受一个回调函数作为参数, 那需要做很多额外的工作来检查是否是可调用的, 正确的回调函数.
而现在, 这个将会变得很简单, Zend Engine提供了一个新的typehint, 来帮助你完成这些检查工作:
是不是方便了很多呢?
另外, 对于扩展开发来说, zend_parse_parameters也提供了对应的新标志:”f”, 可以参看Zend Parameters Parser新增类型描述符介绍
不过还是要提醒下, PHP 5.4还处于开发阶段, 在最终release之前, 任何新特性都可能被调整或者更改. 如果大家有任何建议, 也欢迎反馈, 帮助我们使得PHP变得更好.
在PHP5.4的开发过程中, 有不少对typehint的提议, 我们也讨论过很多次, 完全加入对基本类型的typehint会引起很大的变动, 并且就我自己来看, 不利于PHP的灵活性.
不过, 对于callable类型的支持, 我还是支持的.
在以前, 我们如果希望一个函数接受一个回调函数作为参数, 那需要做很多额外的工作来检查是否是可调用的, 正确的回调函数.
而现在, 这个将会变得很简单, Zend Engine提供了一个新的typehint, 来帮助你完成这些检查工作:
<?php function foo(callable $callback) { }对于下面的例子:
<?php foo("false"); //Catchable fatal error: Argument 1 passed to foo() must be callable * foo("printf"); //okey foo(function(){}); //okey class A { static function show() { } } foo(array("A", "show")); //okey
是不是方便了很多呢?
另外, 对于扩展开发来说, zend_parse_parameters也提供了对应的新标志:”f”, 可以参看Zend Parameters Parser新增类型描述符介绍
不过还是要提醒下, PHP 5.4还处于开发阶段, 在最终release之前, 任何新特性都可能被调整或者更改. 如果大家有任何建议, 也欢迎反馈, 帮助我们使得PHP变得更好.
相关文章推荐
- 函数类型提示(Callable typehint)
- Python 3.5 终于将 Type Hint(类型提示)官方化
- python TypeHint, 类型提示
- Python 3.5 终于将 Type Hint(类型提示)官方化
- python的exit退出时,提示TypeError: 'str' object is not callable
- 编辑控件的警告提示是:This text field does not specify an inputType or a hint
- PHP中的类型提示(type hinting)功能介绍
- VC++API编译提示“与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换”怎么解决?
- Dive Into Python 学习记录2-自省/info 函数 /str / type /callable 函数
- size()函数计算stirng类型长度的用法,注意返回值为size_type类型
- PHP - Manual手册 - Function Reference函数参考 - Variable and Type Related Extensions变量和类型相关扩展 - Variable handling变量操作 - Variable handl
- 错误提示:Uncaught TypeError: xxxxx(此处为要调用的函数) is not a function
- 编辑控件的警告提示是:This text field does not specify an inputType or a hint
- Swift 函数也有类型type
- Eclipse中使用printf报错 提示参数类型不对 已解决The method printf(String, Object[]) in the type PrintStream is not ap
- 关于安卓开发提示:This text field does not specify an inputType or a hint
- 利用 Int2Type 进行函数类型切换
- 创建数据库的时候报错,错误提示为“datatype notmach”,说明数据类型不匹配
- PHP Type Hinting (类型提示)
- Integer(String等函数类型) cannot be resolved to a type 错误解决方法