Page 342 - Computer Software Application TP - Volume 1
P. 342
COMPUTER SOFTWARE APPLICATION - CITS
III Iterating over Objects
1 Open the text editor
2 Write the following codes
<html >
<head>
<title> Iterators </title>
</head>
<body>
<?php
class User
{
public $name;
public $age;
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
}
$users = [
new User(“Alice”, 25),
new User(“Bob”, 30),
];
// Using foreach loop and custom IteratorAggregate:
class UserCollection implements IteratorAggregate
{
private $users;
public function __construct(array $users)
{
$this->users = $users;
}
public function getIterator()
327
CITS : IT & ITES - Computer Software Application - Exercise 60 CITS : IT & ITES - Computer Software Application - Exercise 60