php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47554 call_user_func_array returns NULL (instead of FALSE) for invalid callbacks
Submitted: 2009-03-03 23:47 UTC Modified: 2009-03-04 15:43 UTC
From: jimmy at basicmatrix dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.9 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
49 - 37 = ?
Subscribe to this entry?

 
 [2009-03-03 23:47 UTC] jimmy at basicmatrix dot com
Description:
------------
call_user_func_array() claims it "Returns the function result, or FALSE on error."

My testing shows it returns NULL if the callback function is not a valid callback.

Reproduce code:
---------------
<?php

function callable()
{	return 'Good.';	}

$return = call_user_func_array('callable',array());
var_dump($return);

$return = call_user_func_array('not_callable',array());
var_dump($return);

?>

Expected result:
----------------
string(5) "Good."
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'not_callable' was given in _______ on line 9
FALSE

Actual result:
--------------
string(5) "Good."
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'not_callable' was given in _______ on line 9
NULL

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-04 10:14 UTC] kalle@php.net
Calling a function/method with invalid parameter, eg. parameters that does not pass parameter parsing internally all return NULL. In this case FALSE is returned if something fails after the parameter parsing have passed.
 [2009-03-04 15:43 UTC] jimmy at basicmatrix dot com
I never knew that functions/methods that fail parameter validation will always return NULL. Thanks for clearing that up for me!

But how is it that an uncallable callback is detected as a parameter failure during the internal parameter validation? I would think it would be checking for $callback to be a "string" or "array(2)" (vs. something obviously wrong, i.e., a "resource"). After that, the check for an invalid callback would be detected once call_user_func_array() attempts to call it. In which case, it would "fail" to complete its task and return FALSE.

If the "callback" type is validated fully outside of call_user_func_array()'s control--and if the change I'm suggesting is impossible to make, i.e., if other functions besides call_user_func*() will crash if given an invalid callback--could the documentation for call_user_func* be updated to read:

"Returns the function result, NULL for an invalid callback, and FALSE for all other errors."?

That addition would have saved me a lot of development time. My code was expecting FALSE from call_user_func_array() OR the callback to mean "stop the callback loop" for any reason. (NULL from the callback function is acceptable.) I have fixed my code by calling the is_callable() function first.

Thank for your help!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 08:01:29 2024 UTC