Sunday, November 22, 2009

What is wrong with the following code?

function duplicate($obj) {
 $newObj = $obj;
 return $newObj;
}

$a = new MyClass ( );

$a_copy = duplicate ( $a );

$a->setValue ( 10 );
$a_copy->setValue ( 20 );

Options :

a) You must use return &$newObj instead.
b) There is nothing wrong with this code.
c) duplicate() must accept its parameter by reference.
d) You must use the clone operator to make a copy of an object.
e) duplicate() must return a reference.

References :

Object Cloning

comments to answer

1 comments: