B
B
BonBon Slick2021-06-09 00:04:01
Software design
BonBon Slick, 2021-06-09 00:04:01

Constants for statuses and data types with client-server architecture?

public const TYPES
        = [
            self::TYPE_TV_SERIES,
            self::TYPE_FEATURE_FILM,
            self::TYPE_SHORT_FILM,
        ];
    public const TYPES_STR_CONST
        = [
            self::TYPE_TV_SERIES    => 'TYPE_TV_SERIES',
            self::TYPE_FEATURE_FILM => 'TYPE_FEATURE_FILM',
            self::TYPE_SHORT_FILM   => 'TYPE_SHORT_FILM',
        ];


Which approach would you choose and why?

Personally, I prefer to issue a constant string to the front and use the string, convert it to int and work with int, store int in the database. Let this add complexity in the form of "what and when to use", that is, lines only for working with the client side, but someone can use the constant for other purposes.

public function typeStrConst(): string {
        return EpisodeType::TYPES_STR_CONST[$this->type->value()];
    }
...

        <attribute name="typeStrConst" serialized-name="type">
            <group>non_sensitive_data</group>
            <group>public.info</group>
        </attribute>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Shumov, 2021-06-09
@inoise

In this particular case, these are not constants, but a separate reference book, which has no place in the code from the word at all

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question