How to use enumerations
Changed in version 13.0
The abstract class \TYPO3\
is deprecated.
Classes extending Enumeration
need to be converted into PHP built-in
backed enums.
See Migration to backed enums.
Create an enumeration
To create a new enumeration you have to extend the class
\TYPO3\
. Make sure your enumeration is marked as
final
, this ensures your code only receives a known set of values.
Otherwise adding more values by extension will lead to undefined behavior in
your code.
Values are defined as constants in your implementation. The names of the constants must be given in uppercase.
A special, optional constant __
represents the default value of
your enumeration, if it is present. In that case the enumeration can be
instantiated without a value and will be set to the default.
Example:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Enumerations;
use TYPO3\CMS\Core\Type\Enumeration;
final class LikeWildcard extends Enumeration
{
public const __default = self::BOTH;
/**
* @var int Do not use any wildcard
*/
public const NONE = 0;
/**
* @var int Use wildcard on left side
*/
public const LEFT = 1;
/**
* @var int Use wildcard on right side
*/
public const RIGHT = 2;
/**
* @var int Use wildcard on both sides
*/
public const BOTH = 3;
}
Use an enumeration
You can create an instance of the Enumeration
class like you would
usually do, or you can use the Enumeration::
method for
instantiation. The Enumeration::
method can handle:
Enumeration
instances (where it will simply return the value) and- simple types with a valid
Enumeration
value,
whereas the "normal" __
will always try to create a new
instance.
That allows to deprecate enumeration values or do special value casts before finding a suitable value in the enumeration.
Example:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension;
use MyVendor\MyExtension\Enumerations\LikeWildcard;
final class SomeClass
{
public function doSomething()
{
// ...
$likeWildcardLeft = LikeWildcard::cast(LikeWildcard::LEFT);
$valueFromDatabase = 1;
// will cast the value automatically to an enumeration.
// Result is true.
$likeWildcardLeft->equals($valueFromDatabase);
$enumerationWithValueFromDb = LikeWildcard::cast($valueFromDatabase);
// Remember to always use ::cast and never use the constant directly
$enumerationWithValueFromDb->equals(LikeWildcard::cast(LikeWildcard::RIGHT));
// ...
}
// ...
}
Exceptions
If the enumeration is instantiated with an invalid value, a
\TYPO3\
is thrown.
This exception must be caught, and you have to decide what the appropriate
behavior should be.
Attention
Always be prepared to handle exceptions when instantiating enumerations from user defined values!
Example:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension;
use MyVendor\MyExtension\Enumerations\LikeWildcard;
use TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException;
final class SomeClass
{
public function doSomething()
{
// ...
try {
$foo = LikeWildcard::cast($valueFromPageTs);
} catch (InvalidEnumerationValueException $exception) {
$foo = LikeWildcard::cast(LikeWildcard::NONE);
}
// ...
}
// ...
}
Implement custom logic
Sometimes it makes sense to not only validate a value, but also to have custom logic as well.
For example, the \TYPO3\
enumeration
contains values of version states. Some of the values indicate that the state is
a "placeholder". This logic can be implemented by a custom method:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension;
use TYPO3\CMS\Core\Type\Enumeration;
final class VersionState extends Enumeration
{
public const __default = self::DEFAULT_STATE;
public const DEFAULT_STATE = 0;
public const NEW_PLACEHOLDER = 1;
public const DELETE_PLACEHOLDER = 2;
/**
* @return bool
*/
public function indicatesPlaceholder(): bool
{
return (int)$this->__toString() > self::DEFAULT_STATE;
}
}
The method can then be used in your class:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension;
final class SomeClass
{
public function doSomething()
{
// ...
$myVersionState = VersionState::cast($versionStateValue);
if ($myVersionState->indicatesPlaceholder()) {
echo 'The state indicates that this is a placeholder';
}
// ...
}
// ...
}
Migration to backed enums
Class definition:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Enumeration;
use TYPO3\CMS\Core\Type\Enumeration;
class State extends Enumeration
{
public const STATE_DEFAULT = 'somestate';
public const STATE_DISABLED = 'disabled';
}
should be converted into:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Enumeration;
enum State: string
{
case STATE_DEFAULT = 'somestate';
case STATE_DISABLED = 'disabled';
}
Existing method calls must be adapted.