- assert: documentation ( source)
- var_dump: documentation ( source)
- implode: documentation ( source)
- sprintf: documentation ( source)
<?php
declare(strict_types=1);
class User
{
private const STATUS_ACTIVE = 'active';
private const STATUS_INACTIVE = 'inactive';
public function __construct(public string $username, private string $status = self::STATUS_ACTIVE)
{
}
public function setStatus(string $status): void
{
assert(
in_array($status, [self::STATUS_ACTIVE, self::STATUS_INACTIVE]),
sprintf('Le status %s n\'est pas valide. Les status possibles sont : %s', $status, implode(', ', [self::STATUS_ACTIVE, self::STATUS_INACTIVE]))
);
$this->status = $status;
}
public function getStatus(): string
{
return $this->status;
}
}
$u = new User('Greg');
var_dump($u);