Are objects in php 5 passed by value or reference?

In this code:


When the method Bar::test[] is run and it changes the value of foo # 5 in the array of foo objects, will the actual foo # 5 in the array be affected, or will the $testFoo variable be only a local variable which would cease to exist at the end of the function?

AbraCadaver

77.5k7 gold badges62 silver badges84 bronze badges

asked Jul 9, 2009 at 23:49

1

Why not run the function and find out?

$b = new Bar;
echo $b->getFoo[5]->value;
$b->test[];
echo $b->getFoo[5]->value;

For me the above code [along with your code] produced this output:

Foo #5
My value has now changed

This isn't due to "passing by reference", however, it is due to "assignment by reference". In PHP 5 assignment by reference is the default behaviour with objects. If you want to assign by value instead, use the clone keyword.

answered Jul 9, 2009 at 23:59

Paige RutenPaige Ruten

168k36 gold badges175 silver badges195 bronze badges

4

You can refer to //ca2.php.net/manual/en/language.oop5.references.php for the actual answer to your question.

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.

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

answered Jul 9, 2009 at 23:56

tomzxtomzx

1,9846 gold badges23 silver badges34 bronze badges

They are passed by value in PHP 4 and by reference in PHP 5. In order to pass objects by reference in PHP 4 you have to explicitly mark them as such:

$obj = &new MyObj;

answered Jul 9, 2009 at 23:55

Emil HEmil H

39.3k10 gold badges76 silver badges96 bronze badges

1

One of the key-points of PHP OOP that is often mentioned is that "objects are passed by references by default". This is not completely true. This section rectifies that general thought using some examples.

A PHP reference is an alias, which allows two different variables to write to the same value. In PHP, an object variable doesn't contain the object itself as value. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

Example #1 References and Objects

The above example will output:

miklcct at gmail dot com

12 years ago

Notes on reference:
A reference is not a pointer. However, an object handle IS a pointer. Example:

lazybones_senior

13 years ago

WHOA... KEEP IT SIMPLE!

In regards to secure_admin's note: You've used OOP to simplify PHP's ability to create and use object references. Now use PHP's static keyword to simplify your OOP.



DataModelControl [name=dmc1, data=256]
DataModelControl [name=dmc2, data=256]
DataModelControl [name=dmc3, data=256]

DataModelControl [name=dmc1, data=1024]
DataModelControl [name=dmc2, data=1024]
DataModelControl [name=dmc3, data=1024]

... even better! Now, PHP creates one copy of $data, that is shared amongst all DataModelControl objects.

Are objects passed by reference or value PHP?

In PHP, objects are passed by references by default. Here, reference is an alias, which allows two different variables to write to the same value. An object variable doesn't contain the object itself as value. It only contains an object identifier which allows using which the actual object is found.

Are objects passed by reference?

Object references are passed by value The reason is that Java object variables are simply references that point to real objects in the memory heap. Therefore, even though Java passes parameters to methods by value, if the variable points to an object reference, the real object will also be changed.

Are arrays in PHP passed by reference?

With regards to your first question, the array is passed by reference UNLESS it is modified within the method / function you're calling. If you attempt to modify the array within the method / function, a copy of it is made first, and then only the copy is modified.

How is an object property referenced?

Objects are assigned and copied by reference. In other words, a variable stores not the “object value”, but a “reference” [address in memory] for the value. So copying such a variable or passing it as a function argument copies that reference, not the object itself.

Chủ Đề