How can you effectively use foreach loops to iterate through object properties in PHP?

When iterating through object properties in PHP using foreach loops, you can access the object's properties by using the arrow operator (->). This allows you to loop through each property and its corresponding value. To effectively use foreach loops with objects, you can combine it with the get_object_vars() function to retrieve all the object's properties as an associative array.

// Sample object
class Person {
    public $name = "John";
    public $age = 30;
    public $city = "New York";
}

$person = new Person();

// Iterate through object properties
foreach (get_object_vars($person) as $key => $value) {
    echo "$key: $value\n";
}