#49353 [Bgs]: call_user_array_func()/array_multisort()

From: Date: Tue, 25 Aug 2009 14:54:30 +0000
Subject: #49353 [Bgs]: call_user_array_func()/array_multisort()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-141493@lists.php.net to get a copy of this message
ID: 49353 User updated by: ws at develtheory dot com Reported By: ws at develtheory dot com Status: Bogus Bug Type: Arrays related Operating System: Cent5 PHP Version: 5.3.0 New Comment: Note to all: The solution to this *COUGH* bug is to send all params in as a reference, even though array_multisort clearly shouldn't require reference for any SORT_ args, and values/refs should be respected by call_user_func_array. Realistically, it seems that call_user_func_array is still a very nasty function. The hack-ish diff to make the example code work: -$params[] = $fsort; +$params[] = &$fsort; -$params[] = $fsort_type; +$params[] = &$fsort_type; Previous Comments: ------------------------------------------------------------------------ [2009-08-25 06:42:22] jani@php.net It's not broken, it was fixed. Please read again the explanation by Scott in bug #49241 (it worked by accident in 5.2..) ------------------------------------------------------------------------ [2009-08-25 01:29:10] ws at develtheory dot com Description: ------------ Warning: Parameter 2 to array_multisort() expected to be a reference, value given I realize tickets similar to this (#49069, #49241) have already been marked as bogus, but I'm not finding a good solution or explanation of call_user_func_array's breakage of this between 5.2 and 5.3. The documentation of call_user_func_array describes it as such: "Note: Referenced variables in param_arr are passed to the function by a reference, others are passed by a value. In other words, it does not depend on the function signature whether the parameter is passed by a value or by a reference." This in mind, I'm not sure why SORT_ASC or SORT_REGULAR would _ever_ need to be sent in as a reference. This perplexes me. It seems to me like a properly referenced array should pass in fine. Had weierophinney@php.net written the $args line in OP #49069 as such: $args = array(&$array1, $sort1a, $sort1b, &$array2, $sort2a, $sort2b, &$array3); it should work, right? I have attached example code that in my eyes (and others in #php.pecl and #phpc) should work in 5.3 and does already work in 5.2. Since weierophinney@php.net did not provide a fix in that ticket, could someone point me to a solution, should the functionality still be sans-bug? Reproduce code: --------------- /* first name params */ $fnames = array('9'=>'FirstOne', '2'=>'FirstTwo'); $fsort = SORT_DESC; $fsort_type = SORT_STRING; /* the array itself */ $the_array = array( '9' => array( 'id'=>'9', 'first_name'=>'FirstOne', 'last_name'=>'LastOne' ), '4' => array( 'id'=>'4', 'first_name'=>'FirstTwo', 'last_name'=>'LastTwo' ) ); $params[] = &$fnames; $params[] = $fsort; $params[] = $fsort_type; $params[] = &$the_array; call_user_func_array('array_multisort', $params); //sends warning, doesn't sort properly //array_multisort(&$fnames, $fsort, $fsort_type, &$the_array); //works fine var_dump($the_array); Expected result: ---------------- array 0 => array 'id' => string '4' (length=1) 'first_name' => string 'FirstTwo' (length=8) 'last_name' => string 'LastTwo' (length=7) 1 => array 'id' => string '9' (length=1) 'first_name' => string 'FirstOne' (length=8) 'last_name' => string 'LastOne' (length=7) Actual result: -------------- Parameter 2 to array_multisort() expected to be a reference, value given array 0 => array 'id' => string '9' (length=1) 'first_name' => string 'FirstOne' (length=8) 'last_name' => string 'LastOne' (length=7) 1 => array 'id' => string '4' (length=1) 'first_name' => string 'FirstTwo' (length=8) 'last_name' => string 'LastTwo' (length=7) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=49353&edit=1

« previous php.bugs (#141493) next »