Edit report at https://bugs.php.net/bug.php?id=64266&edit=1
ID: 64266 Comment by: anon at anon dot anon Reported by: stormbyte at gmail dot com Summary: Pass arrays as reference by default Status: Wont fix Type: Feature/Change Request Package: *General Issues Operating System: Linux PHP Version: 5.4.11 Block user comment: N Private report: N New Comment: @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. Previous Comments: ------------------------------------------------------------------------ [2013-02-21 14:55:14] ras...@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-21 14:51:22] johan...@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:22:39] 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) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=64266&edit=1