帮助我理解 PHP 变量引用和范围
引用:
- 如果我将变量传递给函数(例如 $var),它应该是对实际变量的引用的副本(这样将其设置为 null 不会影响其他副本) ?
- 或者它是否接收到对实际变量的新副本的引用(这样将其设置为 null 只会破坏其副本)?
- 如果是后者,这是否会复制内存中的对象和数组?如果是这样的话,这似乎是浪费内存和 CPU 时间的好方法。
我认为首先通过了解其工作原理,可以正确理解按引用传递(例如 &$var)。
范围:
- 本地范围有什么用?我是否正确地观察到,我可以在一个函数中声明一个数组,然后在该函数内调用的其他函数中使用该数组,而不将其作为参数传递给它们?
- 同样,在函数内调用的函数中以数组形式声明是否允许它在调用者中可用?
- 如果不是,范围界定是否可以通过调用堆栈或我所了解的有关编程的每一个该死的事情告诉我它应该工作?
PHP 太有趣了。 :(
References:
- If I pass a variable to a function (e.g. $var), is that supposed to be a copy of a reference to the actual variable (such that setting it null doesn't affect other copies)?
- Or is it receiving a reference to what is a new copy of the actual variable (such that setting it to null destroys its copy only)?
- If the latter, does this copy objects and arrays in memory? That seems like a good way to waste memory and CPU time, if so.
I think can understand passing by reference (e.g. &$var) correctly by knowing how this works, first.
Scope:
- What's the deal with local scope? Am I right in observing that I can declare an array in one function and then use that array in other functions called within that function WITHOUT passing it to them as a parameter?
- Similarly, does declaring in array in a function called within a function allow it to be available in the caller?
- If not, does scoping work by a call stack or whatever like every bloody thing I've come to understand about programming tells me it should?
PHP is so much fun. :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我将变量传递给函数(例如 $var),那么它是否应该是对实际变量的引用的副本(这样将其设置为 null 不会影响其他副本)?
取决于功能。还有你如何称呼它。看这个例子:
http://www.ideone.com/LueFc
或者它是否收到了对什么内容的引用是实际变量的新副本(这样将其设置为 null 只会破坏其副本)?
再次取决于函数
如果是后者,这是否会复制内存中的对象和数组?如果是这样的话,这似乎是浪费内存和 CPU 时间的好方法。
使用引用当然可以节省内存。在 php>4 中,除非您另外指定,否则它总是使用对象的引用。
本地范围有什么用?我是否正确地观察到,我可以在一个函数中声明一个数组,然后在该函数内调用的其他函数中使用该数组,而不将其作为参数传递给它们?
不,你不能。
同样,在函数内调用的函数中以数组形式声明是否允许它在调用者中可用?
不,不是。
如果不是,范围界定是否可以通过调用堆栈或我所了解的有关编程的每一个该死的事情告诉我它应该起作用?
如果你想使用函数外部的变量,在使用它之前,你需要编写
global $outsidevar
If I pass a variable to a function (e.g. $var), is that supposed to be a copy of a reference to the actual variable (such that setting it null doesn't affect other copies)?
Depends on the function. And also how you call it. Look at this example:
http://www.ideone.com/LueFc
Or is it receiving a reference to what is a new copy of the actual variable (such that setting it to null destroys its copy only)?
Again depends on the function
If the latter, does this copy objects and arrays in memory? That seems like a good way to waste memory and CPU time, if so.
Its going to save memory to use a reference, certainly. In php>4 it always uses reference for objects unless you specify otherwise.
What's the deal with local scope? Am I right in observing that I can declare an array in one function and then use that array in other functions called within that function WITHOUT passing it to them as a parameter?
No you can't.
Similarly, does declaring in array in a function called within a function allow it to be available in the caller?
No, it doesn't.
If not, does scoping work by a call stack or whatever like every bloody thing I've come to understand about programming tells me it should?
If you want to use a variable from outside the function, before using it, you'd write
global $outsidevar
关于您的第一组问题:
在这种情况下,
$a
不会被复制到新变量$b
,只是因为它是按值传递的。这是因为 PHP 使用写时复制概念。 PHP 不会复制变量的内容,除非它们被更改。相反,PHP 会增加
$a
现有“zval”的refcount
属性。好吧,整件事并不是那么简单,但要回答你的问题:不,它不会复制变量,除非你在函数中写入它,并且不,你不会通过使用引用来节省 CPU 和内存。在大多数情况下,引用根本不会改变性能,但在最坏的情况下,它实际上会降低性能(因为如果变量的非
is_ref
变体已经存在,并且创建了引用,则值必须复制该变量才能获得带有is_ref
的 zval 和不带is_ref
的 zval)。通过使用引用来优化代码是不好的。Concerning your first set of questions:
In this case,
$a
will not be copied to a new variable$b
, only because it is passed by value.This is because PHP uses the copy-on-write concept. PHP will not copy the contents of a variable, unless they are changed. Instead PHP will increment the
refcount
property of the existing "zval" of$a
.Well, the whole thing is not that trivial, but to answer your question: No, it does not copy the variable, unless you write to it in the function and no, you won't save CPU and Memory by using a reference. In most cases the reference won't change performance at all, but in the worst case it will actually degrade it (because if a not
is_ref
variant of the variable already exists and a reference is created the value of the variable must be copied to get a zval withis_ref
and one without). Optimizing code by using references is no good.如果函数的参数定义为“function my_function($variable) {}”,那么您将获得该变量的副本,并且对函数内的变量所做的任何更改将不可用于函数调用者。您可以在定义函数时通过在参数前添加“&”来通过引用传递变量,因此对变量所做的任何更改都将保留给函数调用者,即“function my_function(&$variable) {}
”参考
if argument to a function is defined as so "function my_function($variable) {}" then you are getting a copy of the variable and any alterations made to the variable inside your function will not be available to the function caller. you can pass a variable by reference by prepending an ampersand to the argument when defining your function and thus any alterations made to the variable will persist to the function caller, ie "function my_function(&$variable) {}"
Passing a variable by reference