<?php
declare(strict_types=1);
class Admin
{
private const STATUS_ACTIVE = 'active';
private const STATUS_INACTIVE = 'inactive';
// Ajout d'un tableau de roles pour affiner les droits des administrateurs :)
public function __construct(public string $username, private array $roles = [], 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;
}
// Méthode d'ajout d'un rôle, puis on supprime les doublons avec array_filter.
public function addRole(string $role): void
{
$this->roles[] = $role;
$this->roles = array_filter($this->roles);
}
// Méthode de renvoie des rôles, dans lequel on défini le role ADMIN par défaut.
public function getRoles(): array
{
$roles = $this->roles;
$roles[] = 'ADMIN';
return $roles;
}
public function setRoles(array $roles): void
{
$this->roles = $roles;
}
}
$a = new Admin('Bobby');
var_dump($a);
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
object(Admin)#1 (3) {
["username"]=>
string(5) "Bobby"
["roles":"Admin":private]=>
array(0) {
}
["status":"Admin":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/BUdlL on line 12
Process exited with code 255.