Augusto Flavio wrote:
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);




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?

4 is the answer. When objects are passed to functions as parameters, any changes made to those objects are done on the objects themselves. Not on the copy of the objects.

When you pass other variables like an array or a string to a function, any changes made to them reflect only within the function's scope.

In PHP 4, if you assigned an object to a variable, actually a copy of that object would be assigned. But not in PHP 5. When you assign an object to a variable no copy is being made. In order to make a duplicate of the object, you have to use the clone operator.


With warm regards,
Sudheer. S
Business:, Tech stuff:, 

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to