How can stdClass objects be effectively accessed and manipulated in PHP?

To access and manipulate stdClass objects in PHP, you can treat them as regular objects by using the arrow (->) operator to access properties and methods. You can also convert stdClass objects to arrays using typecasting or the json_decode function, and then manipulate them as arrays. Additionally, you can dynamically add new properties to stdClass objects by simply assigning values to new property names.

// Create a new stdClass object
$stdObject = new stdClass();
$stdObject->name = 'John';
$stdObject->age = 30;

// Access and manipulate properties
echo $stdObject->name; // Output: John
$stdObject->age = 31;

// Convert stdClass object to array
$array = (array) $stdObject;
print_r($array);

// Add a new property
$stdObject->city = 'New York';
echo $stdObject->city; // Output: New York