Skip to content

Instantly share code, notes, and snippets.

@dasl-
Last active November 21, 2019 18:47
Show Gist options
  • Save dasl-/3043063db918e189c705788877b4720c to your computer and use it in GitHub Desktop.
Save dasl-/3043063db918e189c705788877b4720c to your computer and use it in GitHub Desktop.
<?php declare(strict_types=1);
class MyData implements IteratorAggregate {
public $property1;
public $property2;
public $property3;
public function __construct() {
$this->property1 = new C();
$this->property2 = new D();
$this->property3 = new C();
}
public function getIterator() {
return new ArrayIterator($this);
}
public function anotherMethod() {
}
}
interface I {
function bar();
}
class C implements I {
function bar() {}
}
class D implements I {
function bar() {}
}
$obj = new MyData;
foreach($obj as $key => $value) {
var_dump($key, $value);
echo "\n";
}
foo($obj);
/**
* @param MyData<I> $data
*/
function foo($data) {
foreach ($data as $d) {
$d->bar();
}
$data->anotherMethod();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment