The Stringable interface
The Stringable interface
The interface denotes a class as
having a method. Unlike most interfaces,
is implicitly present on any class that
has the magic method defined, although it
can and should be declared explicitly.
Stringable__toString()Stringable__toString()
Its primary value is to allow functions to type check against the union
type to accept either a string primitive
or an object that can be cast to a string.
string|Stringable
Stringable### Stringable Examples
Voorbeeld: Basic Stringable Usage
This uses .constructor property promotion
<?php
class IPv4Address implements Stringable {
public function __construct(
private string $oct1,
private string $oct2,
private string $oct3,
private string $oct4,
) {}
public function __toString(): string {
return "$this->oct1.$this->oct2.$this->oct3.$this->oct4";
}
}
function showStuff(string|Stringable $value) {
// For a Stringable, this will implicitliy call __toString().
print $value;
}
$ip = new IPv4Address('123', '234', '42', '9');
showStuff($ip);
?>
123.234.42.9