Edit report at https://bugs.php.net/bug.php?id=61467&edit=1
ID: 61467
Comment by: hijarian at gmail dot com
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:
Here's the use case.
I have read the http://php.net/manual/en/language.types.callable.php
and know several syntax variants of specifying callable instances. Among them the array and string
ones.
I want to make higher-order function definition accepting a callable instance.
To both enforce the argument type and provide a type hint for other developers I make the following
definition:
private function wrapper(callable $procedure) {
// pre-actions
$procedure();
// post-actions
}
Next developer creates new method in the same class, and inside it wraps some other method of the
same class:
$this->wrapper([$this, 'someOtherMethod']);
PHP responds with parse error described by OP, and that developer is forced to use anonymous
function instead.
$this->wrapper(function () { $this->someOtherMethod(); });
This can be considered a minor annoyance, but it's explicitly stated in the documentation that
we can specify callable instances by arrays, and [$this, 'someOtherMethod'] *is* callable,
because, let's say, 'someOtherMethod' actually exists in this class.
All your previous discussion is meaningless, because the core of the problem is this: docs say that
you can specify callables as array (and you actually can) but callable type hint DOES
NOT ACCEPT ARRAYS.
Honestly, I don't care whether callable type hint checks actual callability or
not, but I as a programer assume that callable must accept anything
call_user_func accepts, with same behavior. Fatal error is completely unjustified here.
Previous Comments:
------------------------------------------------------------------------
[2013-03-09 20:20:07] sorin dot badea91 at gmail dot com
@nikic I don't think that the use case presented here a minority one. Callbacks
are often send as arguments. The current typehint functionality is completely
useless.
------------------------------------------------------------------------
[2012-12-07 19:18:13] nikic@php.net
@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.
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
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