- URL: https://www.laruence.com/en/2011/10/10/2229.html
- Please include attribution when republishing.
During the development of PHP 5.4, there were quite a few proposals about type hints, and we have discussed them many times. Fully adding type hints for scalar types would cause a lot of change, and in my own view, it's not good for PHP's flexibility.
But I do support the support for the callable type.
In the past, if we wanted a function to accept a callback as an argument, we had to do a lot of extra work to check whether it was a valid, callable function.
Now, this becomes very simple. The Zend Engine provides a new type hint to help you do that kind of checking:
<?php
function foo(callable $callback) {
}
For the following example:
<?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
Isn't that a lot more convenient?
Also, for extension development, zend_parse_parameters provides a corresponding new flag: "f". You can refer to Introducing the New Type Descriptor in the Zend Parameters Parser
That said, a reminder: PHP 5.4 is still under development. Before the final release, any new feature may be adjusted or changed. If you have any suggestions, feedback is welcome, to help make PHP even better.
For more updates, follow: Changelog
Be First to Comment