ZF-5071: Deep clone support please.

Issue Type: Sub-task Created: 2008-11-26T02:25:00.000+0000 Last Updated: 2012-11-20T21:20:13.000+0000 Status: Open Fix version(s): Reporter: wanlei (shannonlee) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Form

  • NeedsPatch

Related issues: Attachments:


Hi Matthew: {color:red} First of all , my id kalon has no permission to create new issue, I had the permission several day ago..what's the reason?{color} You know that Zend_Form has implemented __clone() magic method to support a "senseful" clone operation.Zend_Form knows the fact that clone operation will not copy properties with type of Object , and this fact cause the result clone operation is not do what we expect sometimes. Although __clone in Zend_Form also clone all elements in this form and it's subform and displayGroup , but elements object are just parts of object properties(including decorator,translator,view and so on) in Zend_Form.If we set any object properties or array properties with object values before we clone this form,the result form will not absolutely independent with the previous form.They will hold the same object references with each other.Look over this code:

<pre class="highlight">
$form = new Zend_Form();
$form->addDecorator(new Zend_Form_Decorator_Label());
$newForm = clone $form; 
echo $form->getDecorator('Label');
echo $newForm->getDecorator('Label');

This will output: Object id #14 Object id #14

form and newForm share one Decorator Label, I think this is not the effection we want.Although this is just a simple example, it shows to us we cant use clone in Zend_From safely. The way to solve this problem is to use "deep clone" which can copy properties with type of object ,while the PHP clone operation not.See more about deep clone in PHP5, go ahead my article --
(If you dont know Chinese , just put attention to the PHP code)

Now that we know that PHP function serialize() and unserialize() can help us to do deep clone , we can implement
deepClone() method like this:

<pre class="highlight">
public function deepClone()
    return unserialize(serialize($this)); 

Try the previous code with this deepClone() method:

<pre class="highlight">
$form = new Zend_Form();
 $form->addDecorator(new Zend_Form_Decorator_Label());
$newForm = $form->deepClone(); 
echo ($form->getDecorator('Label'));
echo '<br></br>';
echo ( $newForm->getDecorator('Label'));

Then output: Object id #14 Object id #30

This is the effection we want.

Since __clone() method is published in use , you cant disable it.I've been try my best to find a way by write some unrealistic code in __clone() to make the clone operation has the same effection with deep clone , but failed.

Hope this issue useful.If I've made mistakes in this post,please point out to me .thanks.

(I found a related issue , the way to slove this problem is use deep clone)


Posted by Dolf Schimmel (Freeaqingme) (freak) on 2009-06-13T14:29:20.000+0000

What would be the advantage over cloning each element manually (in e.g. a clone() or deepClone() method)?

Posted by Claude Duvergier (cduv) on 2010-03-15T11:13:10.000+0000

Just ran into the exact same problem.

After cloning a Zend_form_SubForm, the elements of the original SubForm where still using the same instance of Zend_Form_Decorator_Label (for instance).

I had to deep-clone (using : unserialize(serialize($this))) to have a proper clone of my SubForm.

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.