- assert: documentation ( source)
<?php
declare(strict_types=1);
class CanBeStrung
{
public function __toString(): string
{
return 'I\'m highly strung';
}
}
function isItStringy(string|Stringable $possibleStringType): bool
{
return true;
}
assert(isItStringy('a string') === true);
assert(isItStringy(new CanBeStrung()) === true);
try {
assert(isItStringy(42) === false);
} catch (TypeError $e) {
echo '42 isnt a string' . PHP_EOL;
}
try {
assert(isItStringy(new stdClass()) === false);
} catch (TypeError $e) {
echo 'stdClass cant be strung' . PHP_EOL;
}