在PHP中,当$foo = new Foo()时,从技术上来说,$foo是一个对象,还是$foo是一个引用?
更新:在
它说:
PHP5 OOP 的关键点之一是 人们经常提到的是“对象 默认情况下通过引用传递”。 这并不完全正确。
这是为什么呢?以下是一个我不知道是否完全正确的原因:
我认为宽松地说,foo 可以说是一个对象,或者是 Foo 类的一个实例。
但从技术上讲,foo 确实只是一个引用,与 Java 和 Ruby 中完全相同,其中变量 foo
始终只是对对象的引用。
这就是为什么在 PHP 中,
function add($obj) {
$obj->a++;
}
我们不说“通过引用传递”,但从技术上讲,我们正在传递一个值,这是一个引用。因此,它是“传递the引用”,而不是“传递by引用”。
但是,如果我们在 PHP 中说 foo 是一个对象,那么我想“通过引用传递”是有意义的。那么这是真的吗? foo 据说是一个对象的引用,而不是一个对象,所以这就是为什么我们只是“按值传递”?
Update: in
it says:
One of the key-points of PHP5 OOP that
is often mentioned is that "objects
are passed by references by default".
This is not completely true.
Why is that? The following is a reason which I don't know is completely true or not:
I think loosely speaking, foo can be said to be an object, or an instance of the class Foo.
But is it true that very technically speaking, foo is just a reference, the exactly same way in Java and in Ruby, where variable foo
is always just a reference to an object.
So that's why in PHP,
function add($obj) {
$obj->a++;
}
We don't say "pass by reference", but very technically speaking, we are passing a value, which is a reference. So, it is "passing the reference", not "passing by reference".
But, if we say in PHP, that foo is an object, then I guess "passing by reference" can make sense. So is it true? foo is said to be a reference to an object, not an object, so that's why we are just "passing by value"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$foo
不是一个对象,它是一个引用。说$foo
是一个对象是错误的,$foo
是一个指向对象的引用。引用是按值传递的,就像 PHP 中的任何其他参数(Java 和 Ruby 也一样),因此您不能直接分配引用来修改它,但您可以对它指向的对象进行任何操作。
为了简化抽象,程序员有时会说“
$foo
是一个对象”,这是错误的,但它比说整个“$foo
是一个引用”更容易指向一个对象”。在许多情况下,除了某些边缘情况外,差异实际上并不重要。$foo
is not an object, it is a reference. Saying that$foo
is an object is an error,$foo
is a reference that points to an object.References are passed by value, like any other arguments in PHP (Java and Ruby also), so you cannot directly assign the reference to modify it, but you can work however you want on the object it points.
In order to simplify the abstraction, programmers sometimes say that "
$foo
is an object", this is wrong but it is easier than saying the whole "$foo
is a reference that points to an object". In many cases, the difference does not actually matter except in some edge cases.在 PHP 5+ 中,所有对象默认都通过引用传递,在 PHP 5 之前,您使用“&”通过引用传递。运算符
意味着如果将对象传递给方法,则引用将被复制,并且方法内的另一个变量将包含引用的副本。
因此,默认情况下您不会通过引用传递引用:),因此您无法为包含引用副本的变量分配新值并影响原始引用。
All objects are per default passed by reference in PHP 5+, before PHP 5 to pass by reference you used the "&" operator
Meaning that if you pass a object into a method the reference is copied, and another variable inside the method will contain a copy of the reference.
So you are not passing reference's by reference per default :), so you cant assign the variable containing the copy of the reference a new value and affect the original reference.
PHP 对象变量实际上并不包含对象(就像许多其他语言一样) - 它包含对象的“句柄” - 即允许识别对象、找到它并操作它的一些值。该变量像其他变量一样传递 - 它可以通过 val、by-ref 等传递。但是,如果将此值复制到另一个变量,则发生的情况与整数或字符串发生的情况既相同又不同。
整数/字符串:值被复制,因此当它被修改时,另一个保持不变。
对象:值被复制,但该值是句柄。因此,如果此句柄用于访问实际对象(这几乎是使用此句柄的唯一方法)并且此访问修改了它,则另一个变量(独立但包含相同的句柄)将反映更改。
所以它实际上是非常一致的,只是有点不是你所期望的天真地处理“按值”和“按引用”的方式 - 因为传递的内容对于标量和对象变量来说实际上是不同的。对象变量包括一定程度的间接性。
顺便说一句,这就是为什么通过引用传递对象变量几乎没有意义 - 除非您想在函数中用另一个对象替换一个对象。
PHP object variable does not actually contains an object (just as in many other languages) - it contains a "handle" of an object - i.e. some value that allows to identify the object, find it and manipulate it. This variable is passed around like any others - it can be passed by-val, by-ref, etc. However, if you copy this value to another variable, things happen both the same and different from what happens with integer or string.
Integer/String: value is copied, so when it's modified, the other one stays the same.
Object: value is copied, but the value is a handle. So if this handle is used to access the actual object (which is pretty much the only way to make use of this handle) and this access modifies it then the other variable - independent, but containing the same handle - would reflect the change.
So it is actually very consistent, just a bit not what you would expect treating "by value" and "by reference" naively - since what is being passed around is actually different for scalar and object variables. Object variables include a level of indirection.
This is, btw, why it almost never makes sense to pass object variable by-reference - unless you want to replace one object with another in the function.