Issues

ZF-4107: Zend_Config instances should perform deep copy on clone

Issue Type: Bug Created: 2008-08-28T08:52:35.000+0000 Last Updated: 2008-08-29T05:52:42.000+0000 Status: Resolved Fix version(s): - 1.6.1 (15/Sep/08)

Reporter: Matthew Ratzloff (mratzloff) Assignee: Rob Allen (rob) Tags: - Zend_Config

Related issues: Attachments:

Description

From Daniel Skinner

I am getting unexpected behaviour when cloning a Zend_Config object:

Example (actual behaviour):

<pre class="highlight">
$parent = new Zend_Config(array('key' => array('nested' => 'parent')), true); //allow read-write for merging
$newConfig = clone $parent;
$newConfig->merge(new Zend_Config(array('key' => array('nested' => 'override')), true));
echo $newConfig->key->nested; // 'override'  - as expected
echo $parent->key->nested; // 'override' - I was expecting this to be 'parent'

Intuitive behaviour (what makes sense):

Cloning a Zend_Config object should completely separate the new instance from the original - i.e. there should be no cross-references.

<pre class="highlight">
$parent = new Zend_Config(array('key' => array('nested' => 'parent')), true); //allow read-write for merging
$newConfig = clone $parent;
$newConfig->merge(new Zend_Config(array('key' => array('nested' => 'override')), true));
echo $newConfig->key->nested; // 'override'
echo $parent->key->nested; // 'parent'

This is occurring because cloning Zend_Config only creates a shallow clone currently.

Solution 1: Cast to an array and create a new instance:

This can be achieved already and effectively creates a deep clone.

<pre class="highlight">
$parent = new Zend_Config(array('key' => array('nested' => 'parent')), true); //allow read-write for merging
$newConfig = new Zend_Config($parent->toArray(), true); //cast the parent object to an array and create a new Zend_Config
$newConfig->merge(new Zend_Config(array('key' => array('nested' => 'override')), true));
echo $newConfig->key->nested; // 'override'  - as expected
echo $parent->key->nested; // 'parent' - as expected

Solution 2: Fixing Zend_Config to perform a deep clone:

<pre class="highlight">
  /**
   * Perform a deep clone of this instance to allow side-effect free cloning.
   * @return void
   */
  public function __clone()
  {
      $data = array();
      foreach ($this->_data as $key => $value)
      {
          if ($value instanceof Zend_Config)
          {
              $data[$key] = clone $value;
          } else {
              $data[$key] = $value;
          }
      }
      $this->_data = $data;
  }

Gives:

<pre class="highlight">
$parent = new Zend_Config(array('key' => array('nested' => 'parent')), true); //allow read-write for merging
$newConfig = clone $parent;
$newConfig->merge(new Zend_Config(array('key' => array('nested' => 'override')), true));
echo $newConfig->key->nested; // 'override'  - as expected
echo $parent->key->nested; // 'parent' - as expected

It makes sense to me that this is the expected behaviour when cloning and a deep clone greatly reduces the chance of hard to detect side-effects. Does anybody disagree?

A full description of the above can be found at: http://daniel-skinner.co.uk/cloning-zend_config-wi…

Test cases and a patch can be found at: http://destiny-denied.co.uk/files/…

Comments

Posted by Rob Allen (rob) on 2008-08-28T13:44:01.000+0000

Thanks Daniel for doing the research!

Resolved on trunk in svn r11113.

Posted by Rob Allen (rob) on 2008-08-29T05:52:41.000+0000

Committed to release-1.6 branch, svn r11119.

Have you found an issue?

See the Overview section for more details.

Copyright

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

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

Contacts