__toString() Method
__tostring() is a magic method: it is called when the object has to be converted into a string.
That way, it is possible to dynamically convert the object to a string and display it, or transmit it to another part of the application.
__toString() is called when strict_types is not active. Otherwise, passing an object to a string parameter yields an error: it requires an explicit type cast.
__toString() is always called in concatenation, and string interpolation.
There is no magic method to convert an object to another type.
__toString() must be declared public, take no arguments, and return a string: returning anything else triggers a fatal error, or a TypeError when a : string return type is declared.
Since PHP 8.0, any class that defines __toString() automatically implements the Stringable interface, without having to declare it explicitly.
Since PHP 7.4, exceptions may be thrown from within __toString(); in earlier versions, doing so resulted in a fatal error.
<?php
class Elephpant {
private $habitat = 'S';
private $weight = 1;
private $age = 2;
// Checks the dictionary, or return 0
function __toString() : string {
// $this->habitat is used as is
// $this->weight is formatted with quotes
// $this->age is converted to other values
return "$this->habitat '$this->weight' ".($this->age ? 'A' : 'B');
}
}
echo new Elephpant;
$trunk = (string) new Elephpant;
?>