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

PHP 7.4: passing ArrayObject to $view->partial() no longer works #202

Closed
zerocrates opened this issue Dec 3, 2019 · 2 comments · Fixed by #203
Closed

PHP 7.4: passing ArrayObject to $view->partial() no longer works #202

zerocrates opened this issue Dec 3, 2019 · 2 comments · Fixed by #203
Labels
Milestone

Comments

@zerocrates
Copy link

Passing an ArrayObject as the $values parameter to the partial view helper no longer works in PHP 7.4 and zend-view 2.11.3.

The issue is that the Partial class internally uses get_object_vars to handle passed objects that don't have a toArray method:

        } elseif (is_object($values)) {
            if (null !== ($objectKey = $this->getObjectKey())) {
                $values = [$objectKey => $values];
            } elseif (method_exists($values, 'toArray')) {
                $values = $values->toArray();
            } else {
                $values = get_object_vars($values);
            }
        }

In PHP < 7.4, this gets you he wrapped array, but in PHP 7.4 get_object_vars now returns only the direct properties of an ArrayObject, not the contents of its wrapped array. Elsewhere in zend-view, like the variable-setting methods of PhpRenderer and ViewModel, objects implementing Traversable and/or ArrayAccess get specially checked for and handled, but not in the partial() helper.

Obviously, not using an ArrayObject eliminates the issue as well. The only reason I'm personally using an ArrayObject as values for the partial in my real-world use is that I'm using the event manager component to allow the values to be filtered, and prepareArgs results in the arguments being stored/retrieved as an ArrayObject. In previous versions I've been able to pass this directly to the partial() helper without issue.

It's possible to treat this as a 7.4 compatibility issue on the application side rather than the framework side, but as the get_object_vars call at issue lives in the framework, I think it's reasonable to consider this a framework issue.

Code to reproduce the issue

$args = new ArrayObject;
$args['key'] = 'value';
echo $this->partial('partial.phtml', $args);

// in partial.phtml

echo $key;

Expected results

Output:

value

Actual results

Notice: Undefined variable: key in partial.phtml

@michalbundyra
Copy link
Member

@michalbundyra michalbundyra added this to the 2.11.4 milestone Dec 3, 2019
michalbundyra added a commit that referenced this issue Dec 4, 2019
michalbundyra added a commit that referenced this issue Dec 4, 2019
@michalbundyra
Copy link
Member

Thanks, @zerocrates! I am going to release 2.11.4 with the hotfix shortly!

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

Successfully merging a pull request may close this issue.

2 participants