18

in a situation like that below,

class my_class { public __construct($params = array()){ **** do something } } $other_object = new some_class(); $object = new my_class(array( 'var1' => 'test' 'object' => $other_object)); 

$other_object will be passed by reference or by value?

1

5 Answers 5

18

Objects are always references, except you clone it explicitly.

You can use spl_object_hash() to retrieve the "object id" and then compare them against each other. Remember, that once an object is removed from the memory by the garbage collector, the ID may get reused.

Sign up to request clarification or add additional context in comments.

9 Comments

Strange, this was not be behaviour I was experiencing. $tmp = $array[0]; $tmp['item'] = "something"; var_dump($array); \\ original values
@WORMSS Whats the point? You modify $tmp and expect $array to change? Of course this doesn't work.
I would expect it to change, since as you just said Objects are references. And there is an object in $array[0]. You say "Of course this doesn't work" but I am coming back to PHP for the first time in 5 years from doing primarily javascript. Where if you do var tmp = arr[0]; tmp['item'] = "something"; console.log(arr); Arr[0] has been changed since the it is passed by reference. I call it strange behaviour if you say objects are passed as references but this object in an array is acting like a value.
@WORMSS No, it's a primitive type. But the actual question was: What is $array[0]?
Sorry, it was another associative array. But just knowing array is primitive answers all those questions. Like I said, I was coming back to PHP from doing Javascript for a LONG TIME, where arrays are objects. So I was not in the mindset that array in PHP was a primitive. Now I know, it makes a lot of sense.
|
3

Here is one example that shows referenced object every where... http://codepad.org/HK6Oo4xL

1 Comment

did you wrote all that stuff by hand?
0

Objects in php are passed by value. See this answer for details.

1 Comment

php.net/language.references The answer behind your link is something about "PHP doesn't have any references, because there are not like in C". Thats not wrong at all, but because PHP itself describes its understanding of references in its manual in detail, its wrong, when we just stay within the PHP-context. Lets say: An url is a reference to a website. Thats true, but from C's point of view its wrong.
0

Objects in PHP 5 are always passed by reference. With the help of debug_zval_dump() you can check refcount for variable to calculate number of references to the object instance. Pay attention to the note in documentation, you will found many interesting things about passing a variable to a function.

Comments

0

as of PHP 5 PHP object variable contain the reference or identifier to the actual variable. here is an example to demonstrate this.

class test{ public $test = 1; } $obj1 = new test; $orginal = [$obj1,array(2),3]; $copy = $orginal; echo 'orginal array'; var_dump($orginal); echo 'copy of orginal'; var_dump($copy); //after changing $copy[0]->test = 'changed'; $copy[1][0] = 'changed'; $copy[3] = 'changed'; echo 'orginal array after changing its copy'; var_dump($original); echo 'copy of orginal after changing'; var_dump($copy); 

the output for this is

original array array (size=3) 0 => object(test)[38] public 'test' => int 1 1 => array (size=1) 0 => int 2 2 => int 3 copy of original array (size=3) 0 => object(test)[38] public 'test' => int 1 1 => array (size=1) 0 => int 2 2 => int 3 original array after changing its copy array (size=3) 0 => object(test)[38] public 'test' => string 'changed' (length=7) 1 => array (size=1) 0 => int 2 2 => int 3 copy of original after changing array (size=3) 0 => object(test)[38] public 'test' => string 'changed' (length=7) 1 => array (size=1) 0 => string 'changed' (length=7) 2 => string 'changed' (length=7) 

when the object in the copy is changed then the original object is also changed but the array and variable remain unchanged since they are passed as value.

more info about object reference refer : Objects and reference

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.