Edit report at https://bugs.php.net/bug.php?id=64266&edit=1

 ID:                 64266
 Updated by:         johan...@php.net
 Reported by:        stormbyte at gmail dot com
 Summary:            Pass arrays as reference by default
-Status:             Open
+Status:             Not a bug
 Type:               Feature/Change Request
 Package:            *General Issues
 Operating System:   Linux
 PHP Version:        5.4.11
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[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

Reply via email to