<?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);
- Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.13
- object(User)#1 (2) {
["username"]=>
string(4) "Greg"
["status":"User":private]=>
string(6) "active"
}
- Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
- Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting variable (T_VARIABLE) in /in/J3Xss on line 10
Process exited with code 255.
preferences:
67.57 ms | 407 KiB | 5 Q