foreach($objectName as [ $propertyName => ] $propertyValue) {
// statements to execute
}
class Employee {
public $firstName, $lastName;
private $ssn, $dob;
public function __construct($first, $last) {
$this->firstName = $first;
$this->lastName = $last;
}
// getSSN, setSSN, getDOB, setDOB methods not shown
// Show all properties – private, protected, and public public
function showAll() {
echo("<ul>\n");
foreach($this as $name => $value ) {
echo("<li>$name = $value</li>\n");
}
echo("</ul>\n");
}
}
$employee = new Employee('John', 'Doe');
$employee->setSSN('999-14-3456');
$employee->setDOB('3-15-1970');
$employee->showAll();
echo("<ul>\n");
foreach($employee as $name => $value ) {
echo("<li>$name = $value</li>\n");
}
echo("</ul>\n");
foreach loop to access each property in an object.foreach loop coded inside a method of an object loops through the object’s private, protected, and public properties.foreach loop coded outside an object only loops through the object’s public properties.