ZF-6722: Zend_CodeGenerator_Php_Parameter does not allow null as defaultValue


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;


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.

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.

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

Here is a workaround until a patch is committed into ZF core.

h3. Generator Workaround

class Zend_CodeGenerator_Php_Parameter_Value
    protected $_value = NULL;

    public function __construct($value)
        $this->_value = $value;

    public function __toString()
        return $this->_value;

$method = array(
    'name' => '__construct',
    'parameters' => array(
            'name' => 'options',
            'defaultValue' => new Zend_CodeGenerator_Php_Parameter_Value('NULL'),
            'type' => 'array'),
    'body' => '$this->setOptions($options);',

h3. Generated Code


class Default_Model_Quotes

    public function __construct(array $options = NULL)


I hope someone finds this useful.

Fixed in r16344

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.

Reopening due to comments.

Fixed in r18000 and r18001, not yet committed into 1.9 release branch