通过引用返回 [英] Returning by reference

查看:49
本文介绍了通过引用返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PHP 文档中它说:

In the PHP documentation it says:

不要使用按引用返回来提高性能.引擎将自动对其进行优化.

Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own.

我希望返回对数组的引用(这是我的类的属性).PHP如何优化这个,因为数组不是对象?

I wish to return a reference to an array (which is a property of my class). How does PHP optimize this, because the array is not an object?

如果数组有 10 亿个条目,如果我不通过引用传递它,我不会得到两个存储在内存中的 10 亿个条目的数组吗?

If the array has 1 billion entries, won't I get two arrays with 1 billion entries stored in memory if I don't pass it by reference?

推荐答案

PHP 在写入时使用复制.这意味着,如果您将庞大的数组作为函数参数传递并仅从中读取(例如 foreach),您将不会写入它,因此它不需要进行复制.

PHP uses copy on write. That means that if you pass the huge array as a function parameter and only read from it (such as a foreach), you won't be writing to it so it doesn't need to make a copy.

$count = count($huge_array); // read the reference at the bottom
for($i = 0; $i < $count $i++) {
    $value = 2*$huge_array[$i]/15;
    if($value > 3)
        $sub_array []= $value;
}

$subarray 应该更小(它是巨大数组的子集),将只包含所需(更改)的数据.

The $subarray which should be smaller (it is a subset of the huge array), will contain only the needed (changed) data.

如果您不打算更改原始 $huge_array 的值,它将永远不会被复制,因此不会使用额外的内存.

If you do not intend on changing the values of the original $huge_array it will never get copied so no extra memory is used.

如果您打算更改数组的原始值,您需要通过引用传递它.

If you intend on changing the original values of the array, you need to pass it by reference.

如果您打算制作并返回原始数组的更改版本,那么您确实需要 PHP 为您分配的额外内存.

If you intend on making and returning an altered version of the original array then you do need the extra memory PHP is allocating for you.

如果您打算制作并返回原始数组的更改后的较小子集,那么您将创建一个新的空数组,您将从巨大数组中复制一些数据,并应注意不要覆盖 $huge_array 中的值,因此您将避免从 $huge_array 写入并强调从中读取.

If you intend on making and returning an altered smaller subset of the original array then you will create a new empty array into which you will copy some of data from the huge array and should be careful not to overwrite values in the $huge_array, so you'll avoid writing from $huge_array and emphasise on reading from it.

链接 说明 PHP 是针对按值传递用例进行了优化.

This link explains that PHP was optimised for pass by value use cases.

仅当变量不是传递给期望值的函数的引用时,写时复制才有效,如果是,则传递它会触发复制.

Copy on write only works if the variable isn't a reference being passed to a function expecting a value, if it is, passing it around triggers a copy.

这使得期望通过值传递参数并接收引用变量的 PHP 本地函数复制引用的值.

That makes PHP native functions that expected an argument to be passed by value and received a referenced variable copy the value of the reference.

function foo(&$data) {
    for ($i = 0; $i < strlen($data); $i++) {
        do_something($data{$i});
    }
}

$string = "... looooong string with lots of data .....";
foo(string);

在 C 中执行 strlen 意味着迭代整个字符串来计算它.在 PHP 中,字符串有一个附加的长度值.所以 strlen 返回从那里读取的值(快速).

Executing strlen in C would imply iterating over the entire string to count it. In PHP strings have an attached length value. So strlen returns the value read from there (fast).

但是如果你给它一个被引用的变量,它必须在读取它的长度之前复制它,所以它会迭代该值以将它复制到它的参数列表中,读取并返回长度(然后释放内存以供新的复制的字符串).

But if you give it a referenced variable it will have to copy it before reading it's length so it will iterate over the value to copy it into it's argument list, read and return the length (and subsequently release the memory for the freshly copied string).

这篇关于通过引用返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆