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
Posted by Matthew Weier O'Phinney (matthew) on 2011-12-15T18:15:49.000+0000
Honestly, I'd do this differently, and not use a ternary:
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:
Either way, the "workarounds" are actually the appropriate solutions.
Posted by Arron Woods (idealarron) on 2011-12-15T19:29:51.000+0000
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.