<?php
declare(strict_types=1);
class User
{
private const STATUS_ACTIVE = 'active';
private const STATUS_INACTIVE = 'inactive';
public function __construct(private string $username, private string $status = self::STATUS_ACTIVE)
{
}
private 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;
}
private function getStatus(): string
{
return $this->status;
}
}
class Admin extends User
{
public const STATUS_LOCKED = 'locked';
// la méthode est entièrement ré-écrite ici :) seule la signature reste inchangée
public function setStatus(string $status): void
{
assert(
in_array($status, [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_LOCKED]),
sprintf('Le status %s n\'est pas valide. Les status possibles sont : %s', $status, implode(', ', [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_LOCKED]))
);
$this->status = $status;
}
// la méthode utilise celle de la classe parente, et ajoute un comportement :)
public function getStatus(): string
{
return strtoupper(parent::getStatus());
}
}
$admin = new Admin('Paddington');
$admin->setStatus(Admin::STATUS_LOCKED);
echo $admin->getStatus();
Fatal error: Uncaught Error: Undefined constant Admin::STATUS_ACTIVE in /in/2cgVO:39
Stack trace:
#0 /in/2cgVO(55): Admin->setStatus('locked')
#1 {main}
thrown in /in/2cgVO on line 39
Process exited with code 255.
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Parse error: syntax error, unexpected 'private' (T_PRIVATE), expecting variable (T_VARIABLE) in /in/2cgVO on line 10
Process exited with code 255.