Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Auto-generated dictionary using constants #58

Open
Nek- opened this issue Dec 3, 2019 · 3 comments
Open

[RFC] Auto-generated dictionary using constants #58

Nek- opened this issue Dec 3, 2019 · 3 comments
Labels

Comments

@Nek-
Copy link

Nek- commented Dec 3, 2019

We often use constants in dictionary that needs to be set in the constructor, why not having a class that detect constants and use them as keys & values for dictionary ?

Example:

class AwesomeDictionary extends ConstantDictionary
{
    public const MY_VALUE_1 = 'key';
    public const MY_VALUE_2 = 'key2';
    public const MY_VALUE_3 = 'key3';
}
/*
Generates dictionary
[
    'key' => 'MY_VALUE_1',
    'key2' => 'MY_VALUE_2',
    'key3' => 'MY_VALUE_3',
]
*/

What do you think?

@Nek- Nek- added the RFC label Dec 3, 2019
@melicerte
Copy link

Can you elaborate on the use case ? It's not that clear to me.

@Nek-
Copy link
Author

Nek- commented Dec 4, 2019

We often define dictionary the following way:

class NotSoAwesomeDictionary extends SimpleDictionary
{
    public const MY_VALUE_1 = 'key';
    public const MY_VALUE_2 = 'key2';
    public const MY_VALUE_3 = 'key3';

    public function __construct()
    {
         parent::__construct([
            self::MY_VALUE_1 => 'MY_VALUE_1',
            self::MY_VALUE_2 => 'MY_VALUE_2',
            self::MY_VALUE_3 => 'MY_VALUE_3',
        ]);
    }
}

Which doesn't really looks awesome.

But that's not entirely true. It looks in many cases more like:

class NotSoAwesomeDictionary extends SimpleDictionary
{
    public const MY_VALUE_1 = 'key';
    public const MY_VALUE_2 = 'key2';
    public const MY_VALUE_3 = 'key3';

    public function __construct()
    {
         parent::__construct([
            self::MY_VALUE_1 => 'human readable label 1',
            self::MY_VALUE_2 => 'human readable label 2',
            self::MY_VALUE_3 => 'human readable label 3',
        ]);
    }
}

The problem with this approach is that it's not translatable, which is why the first example still makes sense.

@melicerte
Copy link

I see your point. It would be sort of a shortcut instead of using well configured calleable dictionary. Let's do this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants