How to make a copy of an object without reference?

Posted by LaravelIndia - 1 year ago

Best Solutions

<?php
function refObj($object){
    foreach($object as &$o){
        $o = 'this will change to ' . $o;
    }

    return $object;
}

$obj = new StdClass;
$obj->products = 'products';
$obj->cat = 'cat';

$products = clone($obj);

print_r($products)

refObj($obj); // $obj is passed by reference

print_r($products)