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

Changing variables in ViewModel clones are propagated #157

@samuelnogueira

Description

@samuelnogueira

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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions