<?php
class Foo
{
private $iterator;
public function __construct(array $array)
{
$this->iterator = new ArrayIterator($array);
}
public function getIterator()
{
return $this->iterator;
}
}
$reflection = new ReflectionClass('Foo');
$foo = $reflection->newInstanceWithoutConstructor();
dump($foo->getIterator());
// Actually, Foo needs an iterator instead of an array to be injected,
// but if you can't change the class interna:
$array = [
'OneValue',
'TwoValue',
'Three',
'FourValue',
'Five',
];
$iterator = new RegexIterator(
new ArrayIterator($array),
'/Value$/'
);
$prop = $reflection->getProperty('iterator');
// make private propery accessible for reflection
$prop->setAccessible(true);
$prop->setValue($foo, $iterator);
echo get_class($foo->getIterator());