Monday, August 3, 2009

copying a object

Question: What is wrong with the following code?

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
See the book OpenCart 1.4 Template Design Cookbook.
See the book Joomla Mobile Development Beginners Guide




List of my works:

Opencart Extensions:

  1. Product Based Quantity Wise Shipping: Find it here.
  2. OpenSSLCOMMERZ: integrate SSLCOMMERZ with opencart: Find it here.
  3. Fine Search v.1.0 - Improves Opencart search feature to find relevant: Find it here.
  4. Opensweetcaptcha - An easy way to generate attractive captcha for your system!: Find it here.
  5. Custom Field Product - add unlimited custom fields to the product form: Find it here.
  6. Formcaptcha - add captcha on the register page: Find it here.

My Books:

  1. OpenCart 1.4 Template Design Cookbook.
  2. Joomla Mobile Development Beginners Guide

6 comments:

aflavio said...

Is not the "4. You must use the clone operator to make a copy of an object" the answer of this question ?

tahsin said...

first see the question. it is asking "What is wrong with the following code?".So, we must see what is wrong with it.To solve the in the existing code, the answer will be no.3.

thanks for your comments.

__F@bio__ said...

I still thinking that answer 4 is correct.

Thanks for this blog! Its really amazing for people like, who studing for ZCE.

Regards from Brazil

sAm said...

Its not 3, as Objects are always passed by ref, you can test this code, and see what is happening, so if a user really wants to create a duplicate, he must use clone operator.

Anonymous said...

4 is correct!

Anonymous said...

4 is correct indeed