-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectIteration.php
More file actions
39 lines (32 loc) · 995 Bytes
/
Copy pathObjectIteration.php
File metadata and controls
39 lines (32 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
class Data implements IteratorAggregate
{
var string $first = "First";
public string $second = "Second";
private string $third = "Third";
protected string $forth = "Forth";
// dibuat manual dengan implement interface interatorAggregate
// public function getIterator(): Traversable // masih gak tau kenapa harus pakai return traversable
// {
// $array = [
// "first" => $this->first,
// "second" => $this->second,
// "third" => $this->third,
// "forth" => $this->forth,
// ];
// return new ArrayIterator($array);
// }
// menggunakan yield
public function getIterator(): Traversable
{
yield "first" => $this->first;
yield "second" => $this->second;
yield "third" => $this->third;
yield "forth" => $this->forth;
}
}
$data = new Data();
// otomatis
foreach ($data as $property => $value) {
echo "$property : $value" . PHP_EOL;
}