How can constructors in PHP be used to store and retrieve the name of an object?

To store and retrieve the name of an object in PHP, you can utilize a constructor method to set the name during object creation and create a getter method to retrieve the name when needed. This allows you to encapsulate the object's name within the object itself, making it easily accessible without exposing it directly.

class Object {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

$obj = new Object("ExampleObject");
echo $obj->getName(); // Output: ExampleObject