- trim: documentation ( source)
<?php
class StringNotEmptyType {
public string $s;
public function __construct(string $s) {
$s = trim($s);
if ($s === '') {
throw new Exception('String must not be emtpy');
}
$this->s = $s;
}
public function __toString() : string
{
return $this->s;
}
public static function create(string $s) : self
{
return new self($s);
}
}
class Something {
public StringNotEmptyType $s;
}
$okay = new Something();
$okay->s = StringNotEmptyType::create('not empty');
echo $okay->s;
$okay = new Something();
$okay->s = StringNotEmptyType::create('');