Dostęp do metody w klasie abstrakcyjnej

0

Witam,
Mam taki kod:


final class EnumClass extends Enum
{
    protected const ERROR_STATUS = 'kod html [[X]] error';
    protected const WARNING_STATUS = 'kod html [[X]]  warning error';
    protected const SUCCESS_STATUS = 'kod html [[X]]  success error';
}

Enum - jest klasą abstrakcyjną

chciałbym pobrać te dane:

$html = EnumClass::${$messageType}

i otrzymuję komunikat: Uncaught exception: 'Error' with message 'Access to undeclared static property.

W jaki sposób poprawnie to zapisać?

1
  1. zrobiłeś protected więc jak niby chcesz się do tego dostać z zewnątrz
  2. te dane to consty więc tam się $ nie używa
0

Reflection.

<?php

final class EnumClass
{
    protected const ERROR_STATUS = 'foo';
    protected const WARNING_STATUS = 'bar';
    protected const SUCCESS_STATUS = 'baz';
}

$reflection = new ReflectionClass(EnumClass::class);

$messageType = 'WARNING_STATUS';

$const = $reflection->getConstant($messageType);

var_dump($const);

1 użytkowników online, w tym zalogowanych: 0, gości: 1