<?php
use Laminas\Code\Generator\ClassGenerator;
use Laminas\Code\Generator\MethodGenerator;
use Laminas\Code\Generator\PropertyGenerator;
$classGenerator = new ClassGenerator(
'Foo',
'My\Namespace'
);
$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>';
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]]); } }