php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64266 Pass arrays as reference by default
Submitted: 2013-02-21 14:22 UTC Modified: 2013-02-21 14:55 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: stormbyte at gmail dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.4.11 OS: Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: stormbyte at gmail dot com
New email:
PHP Version: OS:

 

 [2013-02-21 14:22 UTC] stormbyte at gmail dot com
Description:
------------
One of major benefits from PHP is that it is very close to C/C++ style, so it is its functions and coding style (very similar for, while and those constructs) so if you come from C/C++ world, you have it easy.

To keep this consistence I suggest, as well as C/C++ does, passing arrays as reference in function arguments by default, or at least an option to behave like that.

For me, it does not make much sense to "follow" C/C++ coding styles and behaviour, while not following that behaviour.

Furthermore, objects are already passed by reference as default, so why not arrays? IMHO I think that inconsistence may confuse programmers.

Test script:
---------------
function foo($arr) {
  array_push($arr, "test");
}

function bar(&$arr) {
  array_push($arr, "test");
}

$a=array();
foo($a);
//$a is empty
bar($a);
//$a[0]="test"

Expected result:
----------------
To be consistent with the rest behaviour of "imitating" C/C++ and pass arrays as reference automatically as well as objects are.
Also, it may be a performance gain by doing that (which is one of the reasons in C world it is that way)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-21 14:51 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 [2013-02-21 14:51 UTC] johannes@php.net
C has no references, but pointers which are a quite different concept. Pass by pointer is neccessary in C to allow maximum performance. In PHP we have copy on write and prefer more intuitive APIs. When reading foo($variable) it is unclear whether $variable will be read only or manipulated, which makes reading code harder. By defaulting to "copies" this is lost.

Also mind that objects are not passed by reference but by handle.

To learn more please see
http://php.net/manual/en/language.references.php
http://php.net/manual/en/features.gc.refcounting-basics.php
http://php.net/manual/en/language.oop5.references.php
http://schlueters.de/blog/archives/125-Do-not-use-PHP-references.html
 [2013-02-21 14:55 UTC] rasmus@php.net
-Status: Not a bug +Status: Wont fix
 [2013-02-21 14:55 UTC] rasmus@php.net
Because PHP uses Copy-On-Write there is actually no performance benefit to 
passing anything by reference in PHP. In fact creating a reference is slower. 
Also, while you are correct that C/C++ passes arrays by reference by default, 
this is mainly because there is simply no way to pass a block of memory by 
value. If you want to make actual changes to the passed array you would still 
have to pass an int** since if you need to change the size of the array and re-
alloc the block of memory you need a pointer to the pointer, so in essence 
passing an int* in C is its form of pass-by-value as it simply avoids doing a 
copy, but doesn't give the receiving function the ability to make real changes 
to the array and passing an int** is C's version of pass-by-reference. This 
exactly mirrors PHP's behaviour. Passing a PHP array by value doesn't make a 
copy of the array because of COW and is thus analogous to passing an int* in C 
and PHP's pass-by-reference is exactly like C's passing of an int**.
 [2013-02-24 03:29 UTC] anon at anon dot anon
@stormbyte
>objects are already passed by reference as default

Nope. Objects in PHP 5+ are not like those in C (which are value-types); PHP objects have implicit reference semantics (same as Java). But parameters are ALWAYS passed by value unless the '&' is there explicitly.

Example:

class Foo {
	public $i;
	public function __construct($i) {
		$this->i = $i;
	}
	public function __toString() { return $this->i . ''; }
}

$obj1 = new Foo(1);
$obj2 = new Foo(2);

$obj = $obj1;
echo $obj;

doSomethingWith($obj);
function doSomethingWith($obj) {
	$obj->i = 3;
	$obj = $GLOBALS['obj2'];
	echo $obj;
}

echo $obj;
echo $obj1;

---------------------

Output is 1233. If PHP passed objects by reference by default, it would be 1223, which is the same value you'll get if you manually add the '&' to the parameter declaration. However, notice obj1 gets modified either way (the last digit) because even the plain assignment ($obj = $obj1) assigns the object address and not the contents.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jun 13 10:01:36 2025 UTC