ZF-7268: Zend_CodeGenerator_Php_Property_DefaultValue shouldn't include semicolon
Description
When writing Code Generation code, I have found it useful to use Zend_CodeGenerator_Php_Property_DefaultValue to build parts of function body code. For example:
require_once 'Zend/CodeGenerator/Php/Class.php';
require_once 'Zend/CodeGenerator/Php/Property/DefaultValue.php';
$elementsArray = array('test1' => 'Description1', 'test2' => 'Description2');
$class = new Zend_CodeGenerator_Php_Class();
$class->setName('SamleClass' );
$temp = new Zend_CodeGenerator_Php_Property_DefaultValue();
$temp->setValue($elementsArray);
$cbody = '$this->addElements(' .$temp->generate(). ');';
$class->setMethod(array(
'name' => '__construct',
'visibility' => 'public',
'body' => $cbody
));
echo $class->generate();
results in
class SamleClass
{
public function __construct()
{
$this->addElements(array(
'test1' => 'Description1',
'test2' => 'Description2'
););
}
}
It could be nice if the semicolon wasn't part of Zend_CodeGenerator_Php_Property_DefaultValue in scenarios like this.
A similar suggestion: If
$temp = new Zend_CodeGenerator_Php_Property_DefaultValue();
$temp->setValue($elementsArray);
could be simplified to
$temp = new Zend_CodeGenerator_Php_Property_DefaultValue($elementsArray);
it would be nice too.
Let me know if there are better ways to accomplish what I'm trying to :-).
Comments
Posted by Matthew Weier O'Phinney (matthew) on 2011-03-23T20:52:26.000+0000
Typically, you shouldn't use CodeGenerator for the body of a method. If you do, Php_Property_DefaultValue is not the appropriate class to use here anyways -- that class is intended for code generation of a class property, not inline-code (Php_Value would be the more appropriate case here).