How to push a copy of an object into array in PHP -


i tried add objects array in php didn't work, tried 2 methods:

#1

$obj->var1 = 'string1'; $obj->var2 = 'string1'; $arr[] = $obj; $obj->var1 = 'string2'; $obj->var2 = 'string2'; $arr[] = $obj; 

#2

$obj->var1 = 'string1'; $obj->var2 = 'string1'; array_push($arr,$obj); $obj->var1 = 'string2'; $obj->var2 = 'string2'; array_push($arr,$obj); 

both methods add latest object entire array. seems object added array reference. there way add array value ?

objects passed reference in php 5 or later. if want copy, can use clone operator

$obj = new myclass; $arr[] = clone $obj; 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -