ZCE question - duplicate object

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



HI everyone,



i'm studying for the zce exam and i found a blog that try to answer some
questions covered in the exam. I found one that i do not agree.

Question: 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);

?>


Answer:

1. You must use return &$newObj instead
2. There is nothing wrong with this code
3. duplicate() must accept its parameter by reference
4. You must use the clone operator to make a copy of an object
5. duplicate() must return a reference

I  think the answer is the number 4 and not 3 because variable referenced in
php 5 is parsed by reference. Am i right?


Thanks



Augusto Morais

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux