ZF-11941: Segmentation fault rendering Zend_Form after condition check

Description

In certain versions of PHP (tested in 5.3.8) and Zend_Framework (tested in 1.11.11), when performing a conditional check on echoing a form instance from Zend_View, it causes PHP to crash.


<?php

require('Zend/Loader/Autoloader.php');
Zend_Loader_Autoloader::getInstance();

$view = new Zend_View();
$form = new Zend_Form;
$form->setView($view);

$input = new Zend_Form_Element_Text('input');
$input->setLabel('Input')
      ->setRequired();
$form->addElement($input);


$view->form = $form;
echo ($view->form)? $view->form : 'nothing';

If I change the last line to the following, it works without failing:


echo ($view->form)? (string) $view->form : 'nothing';

The segmentation fault occurs after the form has rendered in the shutdown phase of PHP.

Any ideas why? I can work around it as described, but it took me a while to track this down!

Comments

Honestly, I'd do this differently, and not use a ternary:


if (isset($view->form)) {
    echo $view->form;
} else {
    echo 'nothing';
}

Since you're not in a view script, I'd actually go a little bit more explicit, and do the following to render the form:


echo $form->render($view);

Either way, the "workarounds" are actually the appropriate solutions.

The code submitted is a test version, quite dissimilar to the application, which has the ternary in a view script.

I concur and am happy to code it the verbose way, but for my own sanity, any idea why this would cause the segmentation fault?

I ran it with gdb but the backtrace meant almost nothing to me, looked like php was trying to unregister resources at shutdown though.