AbstractEnum::tryFrom( string $value ): static|null

In this article

Tries to create an enum instance from a value, returns null if invalid.

Parameters

$valuestringrequired
The enum value.

Return

static|null The enum instance or null.

Source

final public static function tryFrom(string $value): ?self
{
    $constants = static::getConstants();
    foreach ($constants as $name => $constantValue) {
        if ($constantValue === $value) {
            return self::getInstance($constantValue, $name);
        }
    }
    return null;
}

Changelog

VersionDescription
0.1.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.