ZF-6722: Zend_CodeGenerator_Php_Parameter does not allow null as defaultValue
Description
If you set defaultValue as null, then this is not included in the generated output, because the following line resolves as false:
if ($this->_defaultValue) {
To resolve my issue I added a private property to check if null was passed in to the defaultValue and included that in the setDefaultValue method. I then added additional code to the generate method to output null.
/**
* Indicates that defaultValue has been set to null
* @var bool
*/
private $_defaultIsNull = false;
//...
/**
* setDefaultValue()
*
* @param string $defaultValue
* @return Zend_CodeGenerator_Php_Parameter
*/
public function setDefaultValue($defaultValue)
{
$this->_defaultIsNull = (null === $defaultValue);
$this->_defaultValue = $defaultValue;
return $this;
}
// ...
/**
* generate()
*
* @return string
*/
public function generate()
{
$output = '';
if ($this->_type) {
$output .= $this->_type . ' ';
}
$output .= '$' . $this->_name;
if ($this->_defaultValue) {
$output .= ' = ';
if (is_string($this->_defaultValue)) {
$output .= '\'' . $this->_defaultValue . '\'';
} else {
$output .= var_export($this->_defaultValue, true);
}
} elseif ($this->_defaultIsNull) {
$output .= ' = null';
}
return $output;
}
Comments
Posted by Robert Bruce (mightymephisto) on 2009-05-20T02:51:40.000+0000
Actually, just found the issue exists if the defaultValue is set to null, false or 0, so it's better to set a flag to indicate that a defaultValue has been specified and then generate the defaultValue if the flag is true rather than based on the defaultValue itself.
Posted by John Kleijn (448191) on 2009-06-08T10:12:04.000+0000
I wouldn't say this is a minor issue. Anything that prevents Zend_CodeGenerator from producing at least the functional equivalent of what you asked it to, is a mayor issue IMO.
Posted by John Kleijn (448191) on 2009-06-09T05:18:25.000+0000
I created a patch that mimics Reflection_Parameter's behaviour: it adds a isOptional() method (it doesn't throw an error when you try to get a default value on a required parameter though: it just returns NULL).
Path in ZF-6962
Posted by Christian Roy (roychri) on 2009-06-11T11:38:02.000+0000
Here is a workaround until a patch is committed into ZF core.
h3. Generator Workaround
h3. Generated Code
I hope someone finds this useful.
Posted by Ralph Schindler (ralph) on 2009-06-28T13:15:52.000+0000
Fixed in r16344
Posted by Timo Karvinen (timo.karvinen) on 2009-08-12T00:24:14.000+0000
Original issue here talks about Zend_CodeGenerator_Php_Parameter, the fixes are for Zend_CodeGenerator_Php_Property.
I think this issue still exists in Zend_CodeGenerator_Php_Parameter and one cannot give null/false/empty string as default value for function parameters.
Posted by Ralph Schindler (ralph) on 2009-08-12T10:29:26.000+0000
Reopening due to comments.
Posted by Benjamin Eberlei (beberlei) on 2009-09-06T10:42:00.000+0000
Fixed in r18000 and r18001, not yet committed into 1.9 release branch