What is the best practice for displaying the contents of an object in PHP classes?

When displaying the contents of an object in PHP classes, it is best practice to implement a __toString() method within the class. This method should return a string representation of the object's properties that you want to display. This allows for easy and customizable output of object data.

class MyClass {
    public $property1;
    public $property2;

    public function __toString() {
        return "Property 1: " . $this->property1 . ", Property 2: " . $this->property2;
    }
}

$obj = new MyClass();
$obj->property1 = "Value 1";
$obj->property2 = "Value 2";

echo $obj; // Output: Property 1: Value 1, Property 2: Value 2