Edit report at https://bugs.php.net/bug.php?id=64266&edit=1
ID: 64266 Updated by: ras...@php.net Reported by: stormbyte at gmail dot com Summary: Pass arrays as reference by default -Status: Not a bug +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: 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**. Previous Comments: ------------------------------------------------------------------------ [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