php.net |  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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bastard dot internets at gmail dot com
New email:
PHP Version: OS:

 

 [2008-09-10 05:45 UTC] bastard dot internets at gmail dot com
Description:
------------
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:
---------------
<?php

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)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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
Found http://bugs.php.net/bug.php?id=4116

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 15:01:28 2024 UTC