Edit report at https://bugs.php.net/bug.php?id=61467&edit=1
ID: 61467 Comment by: ni...@php.net Reported by: david at grudl dot com Summary: New "callable" typehint does not work (autoloading) Status: Not a bug Type: Bug Package: Class/Object related PHP Version: 5.4.0 Block user comment: N Private report: N New Comment: @paladin / @honza: We certainly won't introduce another type hint just to cover some minority use case. Type hints are there to simplify the most common type checks and callability-without-actual-callability is certainly not one of the common cases. If you have special needs you can always just do the manual is_callable call. Previous Comments: ------------------------------------------------------------------------ [2012-12-07 11:19:16] honza at mujserver dot net I agree with paladin, but I don't think that *callable* is useful. ------------------------------------------------------------------------ [2012-07-16 22:33:06] paladin at jstation dot cz Hmm... I agree with David Grudl that current behavior is not ideal... so what about two typehints? callback and callable? function testA(callback $a) { //callback checks only syntax } function testB(callable $a) { //callable checks if it is really callable } ------------------------------------------------------------------------ [2012-03-27 16:58:41] david at grudl dot com 1) "Yes, something other than a string was expected." Really? What exactly is expected? When a string points to something that _is_ callable, then it is not a string? call_user_func('xxx') triggers error 'expects parameter 1 to be a valid callback, function 'xxx' not found or invalid function name' which is perfectly understandable. Why the same function with typehint callable will not trigger the same error message? It will trigger 'Argument 1 passed to call_user_funct() must be callable, string given', which says that I shouldn't use the string. And that's not simply true. 2) I would totally agree with you, if PHP was static language. But PHP is dynamic language and it makes sense to have stored in the variable name of class/function that has not yet been loaded. And most importantly: the life cycle of PHP is a single HTTP request, lazy loading is very important for good performance. ------------------------------------------------------------------------ [2012-03-24 23:41:57] jan at skrasek dot com There is no logic to check is_callable by typehint - these typehint is useless, brings no new functionality. The only one possibile benefit is just check validity of the callback structure, not the callabality self. > A callable isn't a basic type like a string. So, this typehint behaves differently than others. Yeah, so you prefer to mix things together and make php much more chaotic. > This is what most users expect when they check if something is callable. Some research available? Or just empty words? >From my point of view php is heading to hell by making these type of >shortcuts. Yes, we are lazy programmers, but there is no need to rewrite if >(is_callable(..)) as a typehint. ------------------------------------------------------------------------ [2012-03-23 23:47:03] ras...@php.net 1) Yes, something other than a string was expected. When a string points to something that isn't a callable function, then it is just a string, so I consider the message correct. 2) Yes, I consider it correct. A callable isn't a basic type like a string. It has to have a couple of characteristics, one of which is being actually callable which you can't know unless you check. This is what most users expect when they check if something is callable. If something just looks like a callable function and it isn't, then you end up with an uncatchable fatal when you try to call it, so knowing that something looks like it might be a callable function isn't very useful to most. They want to write robust code and catch any errors and that can only be done if we check for the existence of the callable function which is why it is the default. And your example with array just reinforces this. An array typehint fully checks if an array is an array because it is a simple type that has no other characteristics. An array can't pass the array typehint and then fatal out when you try to use it as an array. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=61467 -- Edit this bug report at https://bugs.php.net/bug.php?id=61467&edit=1