Issues

ZF-6973: Zend_CodeGenerator_Php_Property can't be an Array

Issue Type: Improvement Created: 2009-06-10T01:17:59.000+0000 Last Updated: 2009-06-29T06:46:07.000+0000 Status: Resolved Fix version(s): Reporter: MARTIN Nicolas (cocorambo) Assignee: Ralph Schindler (ralph) Tags: - Zend_CodeGenerator

Related issues: Attachments:

Description

Example:

<pre class="highlight">
new Zend_CodeGenerator_Php_Property
(
    array
    (
        'name'         => '_myProperty',
        'visibility'   => 'protected',
        'defaultValue' => array('value1', 'value2')
    )
)

out in the class

protected $_myProperty = 'array('value1', 'value2')';

So there is "extra" quotes

Comments

Posted by old of Satoru Yoshida (yoshida@zend.co.jp) on 2009-06-11T04:14:28.000+0000

It can not be used with an Array?

Posted by MARTIN Nicolas (cocorambo) on 2009-06-11T04:42:09.000+0000

Add an example

Posted by Søren Haagerup (shaagerup) on 2009-06-27T11:52:53.000+0000

I can confirm this

When calling e.g.

<pre class="highlight">
$class->setProperties(array(
        array(
            'name'         => '_dependentTables',
            'visibility'   => 'public',
            'defaultValue' => array('Test'),
        )));

the only thing that is generated is

<pre class="highlight">
    public $_dependentTables = 'Array';

When looking at the source code

<pre class="highlight">
    public function generate()
    {
        $name         = $this->getName();
        $defaultValue = $this->getDefaultValue();
        if ($this->isConst()) {
            $string = '    ' . 'const ' . $name . ' = \'' . $defaultValue . '\';';
        } else {
            $string = '    ' . $this->getVisibility() . ($this->isStatic() ? ' static' : '') . ' $' . $name . ' = ' . ((null !== $defaultValue) ? '\'' . $defaultValue . '\'' : 'null') . ';';
        }
        return $string; 
    }

it is obvious that it just casts the Array to a string, without doing anything sophisticated. I would expect it to "parse through" the array doing something like

<pre class="highlight">
<?php

$array = array("test", 'test' => "test", "test", array("lalaal", "1234"));
echo parseValue($array) . "\n";

function parseValue($val)
{
    if ($val == null)
    {
        return null;
    }
    else if (is_array($val))
    {
        $entries = array();
        foreach ($val as $key => $entry)
        {
            $entries[] = '\''.$key.'\' => ' . parseValue($entry);
        }
        return 'array(' . implode(", ",$entries) . ')';
    }
    else
    {
        return '\''.$val.'\'';
    }
}
?>

I think I will implement it in my own ZF as a workaround for now. I don't know if there are any better (built-in?) solutions for this?

Posted by Søren Haagerup (shaagerup) on 2009-06-27T12:49:57.000+0000

<pre class="highlight">
        return null;

should of course be

<pre class="highlight">
        return 'null';

Let me know if you consider implementing something like the above in the official ZF - by then, I have likely implemented "pretty indenting" in the function as well..

Posted by Ralph Schindler (ralph) on 2009-06-28T13:15:52.000+0000

Fixed in r16344

Posted by Søren Haagerup (shaagerup) on 2009-06-29T00:53:58.000+0000

Thanks a lot - from the code it looks like it became a little more complicated when pretty-printing etc. was needed :-)

Do you have a recommended way of having objects as part of the default value?

My workaround for now is something like:

$class->setProperties(array(
        array(
            'name'          => 'filters',
            'visibility'    => 'public',
            'defaultValue'  => array(
                new Zend_CodeGenerator_Php_Property_DefaultValue(array(
                    'value' => 'new Zend_Filter_BaseName()',
                    'type'  => Zend_CodeGenerator_Php_Property_DefaultValue::TYPE_NULL
                )),
                new Zend_CodeGenerator_Php_Property_DefaultValue(array(
                    'value' => 'new Zend_Filter_Int()',
                    'type'  => Zend_CodeGenerator_Php_Property_DefaultValue::TYPE_NULL
                ))
        ))));

I am "misusing" the TYPE_NULL for getting it to render the value without quotes.

Posted by Søren Haagerup (shaagerup) on 2009-06-29T01:52:34.000+0000

Dang, what I wanted is actually not supported by PHP. Ignore my comment above for now..

Posted by Ralph Schindler (ralph) on 2009-06-29T06:46:07.000+0000

Ha, yeah. Property default values can only be scalar, constant and null. You'd want to populate with real objects at construction time. Rule of thumb is that CodeGenerator produces valid PHP code, so it attempts to play by all the rules of the language.

-ralph

Have you found an issue?

See the Overview section for more details.

Copyright

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

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

Contacts