Zend Code: create arbitrary PHP classes with this simple generator

Code
<?php

use Zend\Code\Generator\ClassGenerator;
use 
Zend\Code\Generator\MethodGenerator;
use 
Zend\Code\Generator\PropertyGenerator;

$classGenerator = new ClassGenerator(
    
'Foo',
    
'My\Namespace',
    
'flag'
);

$classGenerator->setImplementedInterfaces(
    [
'IteratorAggregate']
);
$classGenerator->addProperties([
    new 
PropertyGenerator('bar'1),
    
'baz',
]);
$classGenerator->addUse('IteratorAggregate');
$classGenerator->addUse('ArrayIterator');

$constructMethod = new MethodGenerator('__construct', ['baz']);
$constructMethod->setBody('$this->baz = $baz;');

$getIteratorMethod = new MethodGenerator('getIterator');
$getIteratorMethod->setBody(
    
'return new ArrayIterator([$this->baz, $this->bar]]);'
);

$classGenerator->addMethods([
    
$constructMethod,
    
$getIteratorMethod,
]);

echo 
'<pre>'.$classGenerator->generate().'</pre>';
Result
namespace My\Namespace;

use IteratorAggregate;
use ArrayIterator;

class Foo implements IteratorAggregate
{

    public $bar = 1;

    public $baz = null;

    public function __construct($baz)
    {
        $this->baz = $baz;
    }

    public function getIterator()
    {
        return new ArrayIterator([$this->baz, $this->bar]]);
    }


}
Used Versions
PHP 7.1, Zend 3.0, Symfony 3.2, Laravel 5.2, PHPUnit 5.6, Doctrine 2.6