在php中创建通过引用传递的变量的实际副本

在php中创建通过引用传递的变量的实际副本

问题描述:

Actually, I have a function where a certain variable is passed as argument by reference. I want to create an actual copy of this variable inside my function instead of having a reference. How can I accomplish this in php?

实际上,我有一个函数,其中某个变量作为参数通过引用传递。 我想在我的函数中创建这个变量的实际副本而不是引用。 我怎样才能在php中完成这个? p> div>

References in PHP do not work as pointers; actually variables in PHP are zval structures, and they contain information for the ref count, is the variable a reference and so on. This works transparently for you, and all that matters when you are using a reference is that you are modifying the original object, and possibly use less memory.

So, if you want to work with a fresh copy of the variable, to be safe from modifications, you can do:

$new_copy = $copy;

or if $copy is an object:

$new_copy = clone $copy;