How can the functionality of accessing an array without an index be replicated in PHP using magic methods like __toString?

To replicate the functionality of accessing an array without an index in PHP using magic methods like __toString, we can create a class that stores an array as a property and implements the __toString method to return the entire array as a string when the object is treated as a string.

class ArrayWrapper {
    private $array = [];

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

    public function __toString() {
        return implode(', ', $this->array);
    }
}

$array = new ArrayWrapper([1, 2, 3, 4]);
echo $array;