Is it considered bad practice to pass items in a PHP array by reference instead of by value?
Relevant documentation: http://php.net/manual/en/control-structures.foreach.php
|
Is it considered bad practice to pass items in a PHP array by reference instead of by value? Relevant documentation: http://php.net/manual/en/control-structures.foreach.php |
|||||
|
|
It's not harmful if it's appropriate for the use case. If you are concerned:
Can also be written as:
Note that as indicated by animuson in a comment, and the big fat warning on the docs page, if you leave a reference lying around and reuse the variable name - you're going to update what you are referencing. That doesn't mean references are harmful; it just means learn how to use a tool - and then use it correctly. |
|||
|
|
|
References are a way to have multiple variables referencing the same variable container using different names -- so whatever name you're using an operation on that variable will always have an effect on the others.So generally we avoid |
|||
|
No, it isn't considered bad practice. If it was bad in any way, then it wouldn't exist in the first place. What is bad is when programmers misuse references or use them where they aren't needed. You cannot answer simply whether it's bad or good to use or avoid references. You'll always get a few guys in "don't use references" camp that will provide a disaster scenario where your code blows up, and then you'll have another camp of people showing benchmark results with and without references. Bottom line: use them where appropriate. Don't rely on random blogs that invent a bogus scenario where references are bad, especially if that scenario has no real world application. |
|||
|
|