Can typecasting an object into an array using (array)$object be a simpler alternative in PHP?
Typecasting an object into an array using (array)$object can be a simpler alternative in PHP to convert an object into an array. This method automatically converts the object's properties into array keys and values, making it convenient for accessing object data in array format.
// Example object
class Person {
public $name = 'John';
public $age = 30;
}
$person = new Person();
// Typecasting object into an array
$array = (array)$person;
// Accessing object data in array format
echo $array['name']; // Output: John
echo $array['age']; // Output: 30