Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Changing variables in ViewModel clones are propagated #157

Closed
samuelnogueira opened this issue Jul 20, 2018 · 1 comment
Closed

Changing variables in ViewModel clones are propagated #157

samuelnogueira opened this issue Jul 20, 2018 · 1 comment

Comments

@samuelnogueira
Copy link
Contributor

By default, \Zend\View\Model\ViewModel::$variables is a \Zend\View\Variables instance.
When cloning an instance of ViewModel, if $variables property is a reference to an object instead of a native php array, calling setVariables on the clone will also change the variables in the original ViewModel instance.
This becomes a problem when using zend-view as a view renderer in Zend Expressive on a long-running process (see https://github.com/zendframework/zend-expressive-zendviewrenderer/blob/2.0.0/src/ZendViewRenderer.php#L319), and causes layout variables to be stuck in the same value between requests.

How to reproduce:

<?php
use \Zend\View\Model\ViewModel;

$viewModel1 = new ViewModel();
$viewModel1->setVariables(['a' => 'foo']);

$viewModel2 = clone $viewModel1;
$viewModel2->setVariables(['a' => 'bar']);

echo $viewModel1->a;

// outputs 'bar'
// expected output is 'foo'
@weierophinney
Copy link
Member

Fixed with #158, to release with 2.11.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants