Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are objects in array passed by reference?

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?

like image 754
alesdario Avatar asked Jul 07 '11 09:07

alesdario


People also ask

Are array elements passed by reference?

Longer answer: Like all Java objects, arrays are passed by value ... but the value is the reference to the array. So, when you assign something to a cell of the array in the called method, you will be assigning to the same array object that the caller sees. This is NOT pass-by-reference.

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.

Is array always passed by reference in C?

Arrays are always pass by reference in C. Any change made to the parameter containing the array will change the value of the original array.


1 Answers

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.

like image 75
KingCrunch Avatar answered Oct 16 '22 00:10

KingCrunch