Closed basz closed 8 years ago
How are you currently trying it?
On 29 Sep 2016 00:05, "Bas Kamer" notifications@github.com wrote:
Example of how to generate stuff like so...
ClassName::SOME_CONSTANT = ...
all I seem to manage is
'ClassName::SOME_CONSTANT' = ...
I would like to know how to do this and when I do I will add it as example to the documentation.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/zendframework/zend-code/issues/96, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJakGV84upTkQA2Yzcx_HWc4ylmkSHEks5quuSagaJpZM4KJXgh .
I gave up because I couldn't find a solution... back to str_replace and heredocs
$classGenerator = new \Zend\Code\Generator\ClassGenerator();
$propertyGenerator = new \Zend\Code\Generator\PropertyGenerator();
$value = new \Zend\Code\Generator\ValueGenerator();
$value->setValue('ClassName::SOME_CONSTANT');
$value->setType(\Zend\Code\Generator\ValueGenerator::TYPE_CONSTANT);
$propertyGenerator->setName("foo");
$propertyGenerator->setDefaultValue($value->generate());
$classGenerator->addPropertyFromGenerator($propertyGenerator);
print $classGenerator->generate();
class
{
public $foo = 'ClassName::SOME_CONSTANT';
}
That's not a value, but an expression. AFAIK, that doesn't really work with the value generator.
Plus I'm fairly sure that most default value expressions were overhauled in 5.6 :-\
On 29 Sep 2016 09:00, "Bas Kamer" notifications@github.com wrote:
I gave up because I couldn't find a solution... back to str_replace and heredocs
$classGenerator = new \Zend\Code\Generator\ClassGenerator(); $propertyGenerator = new \Zend\Code\Generator\PropertyGenerator(); $value = new \Zend\Code\Generator\ValueGenerator(); $value->setValue('ClassName::SOME_CONSTANT'); $value->setType(\Zend\Code\Generator\ValueGenerator::TYPE_CONSTANT); $propertyGenerator->setName("foo"); $propertyGenerator->setDefaultValue($value->generate());
$classGenerator->addPropertyFromGenerator($propertyGenerator);
print $classGenerator->generate();
class {
public $foo = 'ClassName::SOME_CONSTANT';
}
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/zendframework/zend-code/issues/96#issuecomment-250386472, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJakB2IMWdoztEMH9bXgJ-lGbcTAqczks5qu2IPgaJpZM4KJXgh .
PropertyGenerator::setDefaultValue
expects a PropertyValueGenerator
instance. In your code I changed line 3 from ValueGenerator
to PropertyValueGenerator
:
$classGenerator = new \Zend\Code\Generator\ClassGenerator();
$propertyGenerator = new \Zend\Code\Generator\PropertyGenerator();
$value = new \Zend\Code\Generator\PropertyValueGenerator();
$value->setType(\Zend\Code\Generator\ValueGenerator::TYPE_CONSTANT);
$value->setValue('ClassName::SOME_CONSTANT');
$propertyGenerator->setName("foo");
$propertyGenerator->setDefaultValue($value);
$classGenerator->addPropertyFromGenerator($propertyGenerator);
print $classGenerator->generate();
Result:
class
{
public $foo = ClassName::SOME_CONSTANT;
}
@adamlundrigan neat! Closing here then!
Example of how to generate stuff like so...
all I seem to manage is
I would like to know how to do this and when I do I will add it as example to the documentation.