Last active
November 21, 2019 18:47
-
-
Save dasl-/3043063db918e189c705788877b4720c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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