|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-03-21 20:22 UTC] david at grudl dot com
Description:
------------
Is really new type hint callable implemented? I see no difference between PHP 5.3 and PHP 5.4, both versions only throw catchable fatal errors.
(I think this unexpected behaviour is due to the fact that class "A" do not exists. In this case the error message is confusing. But the callable should not trigger autoload, it should behave like is_callable($arg, TRUE) and just check the syntax. Otherwise typehint callable will cause major performance issues.)
Test script:
---------------
function test(callable $a)
{
}
test(array('A', 'b'));
// Catchable fatal error: Argument 1 passed to test() must be an instance of callable, array given
test('A::b');
// Catchable fatal error: Argument 1 passed to test() must be an instance of callable, string given
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 06 03:00:01 2025 UTC |
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.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 }