简体   繁体   English

php返回或引用?

[英]php return or reference?

I have got a couple of functions that manipulate data in an array, for example unset_data() you pass it the value and an unlimited amount of string args like: 我有一些操作数组中数据的函数,例如unset_data()你传递的值和无限量的字符串args如:

unset_data( $my_array, "firstname", "password" );

and it can handle multi-dimentional arrays etc, quite simple. 它可以处理多维数组等,非常简单。

But should this function use a reference to the array and change it directly? 但是这个函数应该使用对数组的引用并直接更改吗?

Or should i return the new array with the values unset. 或者我应该返回具有未设置值的新数组。

I can never decide whether a function should use reference or not, 我永远无法决定函数是否应该使用引用,

Is there like, specific cases or examples when and when to not use them?? 是否有特定情况或例子何时何地不使用它们?

Thanks 谢谢

Generally speaking, it's better to return by value instead of taking a reference because: 一般来说,最好按值返回而不是参考,因为:

  1. It's the most common usage pattern (there's one less thing to keep in mind about this particular function) 这是最常见的使用模式(关于这个特定功能,请记住一点)
  2. You can create call chains freely, eg you can write array_filter(unset_data(...)) 你可以自由创建调用链,例如你可以编写array_filter(unset_data(...))
  3. Generally speaking, code without side effects (I 'm calling the mutation of an argument in a manner visible to the caller a side effect) is easier to reason about 一般来说,没有副作用的代码(我以调用者可见的方式调用参数的变异产生副作用)更容易推理

Most of the time, these advantages come at the cost of using up additional memory. 大多数情况下,这些优势是以耗尽额外内存为代价的。 Unless you have good reason (or better yet, proof) to believe that the additional memory consumption is going to be an issue, my advice is to just return the mutated value. 除非你有充分的理由(或者更好,证明)相信额外的内存消耗将是一个问题,我的建议是只返回变异的值。

I'd ask myself what the expected use case of the function is. 我会问自己函数的预期用例是什么。 Does the typical use case involve keeping the original data intact and deriving new data from it, or is the explicit use case of this function to modify data in place? 典型的用例是否涉及保持原始数据的完整性并从中获取新数据,或者是此函数的明确用例来修改数据?

Say md5 would modify data in place, that would be pretty inconvenient, since I usually want to keep the original data intact. 假设md5会修改到位的数据,这会非常不方便,因为我通常希望保持原始数据的完整性。 So I'd always have to do this: 所以我总是要这样做:

$hash = $data;
md5($hash);

instead of: 代替:

$hash = md5($data);

That's pretty ugly code, forced on you by the API of the function. 这是非常难看的代码,由函数的API强加给你。

For unset though, I don't think the typical use case is for deriving new data: 但是,对于未unset ,我不认为典型的用例是用于获取新数据:

$arr = unset($arr['foo']);

That seems pretty clunky as well as possibly a performance hit. 这似乎非常笨重,也可能是性能损失。

I feel that there is not a general you should/shouldn't answer to this question - it depends entirely on the usage case. 我觉得你不应该/不应该回答这个问题 - 这完全取决于使用案例。

My personal feeling is leaning towards passing by reference, to keep it's behaviour more in line with the native unset() , but if you are likely to end up regularly having to make copies of the array before you call the function, then go with a return value. 我个人的感觉是倾向于通过引用传递,以保持它的行为更符合本机unset() ,但如果您可能最终定期在调用函数之前必须复制数组,那么请使用回报价值。 Another advantage of the by reference approach is that you can return some other information as well as achieving modification of the array - for example, you could return an integer describing how many values were removed from the array based on the arguments. 引用方法的另一个优点是您可以返回一些其他信息以及实现对数组的修改 - 例如,您可以返回一个整数,该整数描述基于参数从数组中删除了多少个值。

I don't think there is a solid argument for "best practice" with either option here, so the short answer would be: 我不认为这里有任何选项的“最佳实践”都有一个坚实的论据,所以简短的答案是:

Do whatever you are most comfortable with and whatever allows you to write the most concise, readable and self-documenting code. 做任何你最熟悉的事情,以及任何允许你编写最简洁,可读和自我记录的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM