|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46037 array_walk not passing third parameter by reference
Submitted: 2008-09-10 05:45 UTC Modified: 2008-09-10 16:36 UTC
From: bastard dot internets at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: n/a OS: n/a
Private report: No CVE-ID: None
 [2008-09-10 05:45 UTC] bastard dot internets at gmail dot com
When array_walk function is used inside __construct function of an object, array_walk allows passing of its third parameter to the user defined function by reference.  This is expected.

In any other case, function, or scope, this parameter cannot be passed by reference, instead passing by value yet producing no error even when the user defined function demands passing this third parameter by reference.  Possibly similar to bug 45780, but unlikely due to function scope behaviour.

Testing on php 5.2.4 (cvs/snap unknown); apache 2.2; ubuntu 8.04 server.  PHP packaged with ubuntu 8.04 lamp.

Reproduce code:

class ClassTest {
	public $val_1 = null;
	public $val_2 = null;

	function __construct($array) {
		$class_test_array = array("val_1" => 1, "val_2" => 2, "val_3" => 3);
		array_walk($class_test_array,create_function('$v,$k,&$that','if (property_exists($that,$k)) {$that->$k = $v;}'),$this);


function alter_test_array($val, $key, &$test_array) {
	$test_array[] = $val;

$class_test_obj = new ClassTest();
$normal_test_array_1 = array(1,2,3);
$normal_test_array_2 = array();
$normal_test_array_3 = array();

array_walk($normal_test_array_1, create_function('$val,$key,&$test_array', '$test_array[] = $val;'), $normal_test_array_2);
array_walk($normal_test_array_1, alter_test_array, $normal_test_array_3);

Expected result:
$class_test_obj === object(1,2)
$normal_test_array_2 === array(1,2,3)
$normal_test_array_3 === array(1,2,3)

Actual result:
$class_test_obj === object(1,2)
$normal_test_array_2 === array(0)
$normal_test_array_3 === array(0)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-10 06:44 UTC] bastard dot internets at gmail dot com
Fixing email contact.
 [2008-09-10 16:29 UTC] bastard dot internets at gmail dot com

Not a bug, but working as it should.  Third parameter must be passed by reference into array_walk function itself to be passed by reference on to the callback function.

Closing case.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Jan 20 00:01:23 2021 UTC