ZF-6722: Zend_CodeGenerator_Php_Parameter does not allow null as defaultValue

Issue Type: Sub-task Created: 2009-05-19T03:01:16.000+0000 Last Updated: 2009-09-09T00:40:52.000+0000 Status: Resolved Fix version(s): - 1.9.3 (22/Sep/09)

Reporter: Robert Bruce (mightymephisto) Assignee: Benjamin Eberlei (beberlei) Tags: - Zend_CodeGenerator

Related issues: - ZF-7804



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;


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

<pre class="highlight">
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

<pre class="highlight">

class Default_Model_Quotes

    public function __construct(array $options = NULL)


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

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.