<?php
declare(strict_types=1);
abstract class User
{
public const STATUS_ACTIVE = 'active';
public const STATUS_INACTIVE = 'inactive';
public function __construct(public string $email, public 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;
}
abstract public function getUsername(): string;
}
final class Admin extends User
{
public const STATUS_ACTIVE = 'is_active';
public const STATUS_INACTIVE = 'is_inactive';
// Ajout d'un tableau de roles pour affiner les droits des administrateurs :)
public function __construct(string $email, string $status = self::STATUS_ACTIVE, public array $roles = [])
{
parent::__construct($email, $status);
}
public function getUsername(): string
{
return $this->email;
}
// ...
}
$admin = new Admin('michel@petrucciani.com');
var_dump($admin);
$admin->setStatus(Admin::STATUS_INACTIVE);
object(Admin)#1 (3) {
["email"]=>
string(22) "michel@petrucciani.com"
["status"]=>
string(9) "is_active"
["roles"]=>
array(0) {
}
}
Fatal error: Uncaught AssertionError: Le status is_inactive n'est pas valide. Les status possibles sont : active, inactive in /in/VKRO8:16
Stack trace:
#0 /in/VKRO8(16): assert(false, 'Le status is_in...')
#1 /in/VKRO8(53): User->setStatus('is_inactive')
#2 {main}
thrown in /in/VKRO8 on line 16
Process exited with code 255.
Output for 8.1.0 - 8.1.30
object(Admin)#1 (3) {
["email"]=>
string(22) "michel@petrucciani.com"
["status"]=>
string(9) "is_active"
["roles"]=>
array(0) {
}
}
Fatal error: Uncaught AssertionError: Le status is_inactive n'est pas valide. Les status possibles sont : active, inactive in /in/VKRO8:18
Stack trace:
#0 /in/VKRO8(18): assert(false, 'Le status is_in...')
#1 /in/VKRO8(53): User->setStatus('is_inactive')
#2 {main}
thrown in /in/VKRO8 on line 18
Process exited with code 255.
Output for 8.0.0 - 8.0.30
object(Admin)#1 (3) {
["roles"]=>
array(0) {
}
["email"]=>
string(22) "michel@petrucciani.com"
["status"]=>
string(9) "is_active"
}
Fatal error: Uncaught AssertionError: Le status is_inactive n'est pas valide. Les status possibles sont : active, inactive in /in/VKRO8:18
Stack trace:
#0 /in/VKRO8(18): assert(false, 'Le status is_in...')
#1 /in/VKRO8(53): User->setStatus('is_inactive')
#2 {main}
thrown in /in/VKRO8 on line 18
Process exited with code 255.
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/VKRO8 on line 10
Process exited with code 255.