Adding additional attributes to an Enum object











up vote
-2
down vote

favorite












I have used Enum objects before in PHP as shown here.



However, I often find it's a requirement to have further attributes or metadata available on the instances.



I came up with the example solution below:



<?php

class Month {
const January = 1;
const February = 2;
const March = 3;
const April = 4;
const May = 5;
const June = 6;
const July = 7;
const August = 8;
const September = 9;
const October = 10;
const November = 11;
const December = 12;

protected $value;

protected static $metadata = [
1 => ['days_in_month' => 31],
2 => ['days_in_month' => 28],
3 => ['days_in_month' => 31],
4 => ['days_in_month' => 30],
5 => ['days_in_month' => 31],
6 => ['days_in_month' => 30],
7 => ['days_in_month' => 31],
8 => ['days_in_month' => 31],
9 => ['days_in_month' => 30],
10 => ['days_in_month' => 31],
11 => ['days_in_month' => 30],
12 => ['days_in_month' => 31]
];

public function __construct($value)
{
$this->value = $value;
}

public function __get($name)
{
if (array_key_exists($name, static::$metadata[$this->value])) {
return static::$metadata[$this->value][$name];
}
}
}

$month = new Month(Month::February); // Returns Month instance
$month->days_in_month; // Returns 28 (for February)


Obviously the metadata array can be expanded to include further attributes that should be available for each enumerable option. And of course this need to throw exceptions / handle problems.



Feedback appreciated. Is there a better way to have an Enumerable-type approach which can include additional attributes?










share|improve this question
























  • What are you using this for, where April to December aren't needed?
    – Toby Speight
    Nov 12 at 17:32










  • I used three months to keep the script short and easy to read - it's an example, you can use imagination to fill out the rest ;)
    – benjaminhull
    Nov 12 at 17:44










  • There is a serious point hidden in my tongue-in-cheek remark - here on Code Review, we prefer complete code where we don't have to imagine what you wrote. (That said, it's good that you include February, which doesn't always have 28 days).
    – Toby Speight
    Nov 12 at 17:48










  • As you seem to be a Stack Overflow user, it's worth reading A guide to Code Review for Stack Overflow users. It will tell you what's expected here that's different to what you're used to.
    – Toby Speight
    Nov 12 at 17:50






  • 2




    Thanks for the intro. I have completed the code. Some constructive feedback around the problem I'm trying to solve would be helpful.
    – benjaminhull
    Nov 13 at 9:23















up vote
-2
down vote

favorite












I have used Enum objects before in PHP as shown here.



However, I often find it's a requirement to have further attributes or metadata available on the instances.



I came up with the example solution below:



<?php

class Month {
const January = 1;
const February = 2;
const March = 3;
const April = 4;
const May = 5;
const June = 6;
const July = 7;
const August = 8;
const September = 9;
const October = 10;
const November = 11;
const December = 12;

protected $value;

protected static $metadata = [
1 => ['days_in_month' => 31],
2 => ['days_in_month' => 28],
3 => ['days_in_month' => 31],
4 => ['days_in_month' => 30],
5 => ['days_in_month' => 31],
6 => ['days_in_month' => 30],
7 => ['days_in_month' => 31],
8 => ['days_in_month' => 31],
9 => ['days_in_month' => 30],
10 => ['days_in_month' => 31],
11 => ['days_in_month' => 30],
12 => ['days_in_month' => 31]
];

public function __construct($value)
{
$this->value = $value;
}

public function __get($name)
{
if (array_key_exists($name, static::$metadata[$this->value])) {
return static::$metadata[$this->value][$name];
}
}
}

$month = new Month(Month::February); // Returns Month instance
$month->days_in_month; // Returns 28 (for February)


Obviously the metadata array can be expanded to include further attributes that should be available for each enumerable option. And of course this need to throw exceptions / handle problems.



Feedback appreciated. Is there a better way to have an Enumerable-type approach which can include additional attributes?










share|improve this question
























  • What are you using this for, where April to December aren't needed?
    – Toby Speight
    Nov 12 at 17:32










  • I used three months to keep the script short and easy to read - it's an example, you can use imagination to fill out the rest ;)
    – benjaminhull
    Nov 12 at 17:44










  • There is a serious point hidden in my tongue-in-cheek remark - here on Code Review, we prefer complete code where we don't have to imagine what you wrote. (That said, it's good that you include February, which doesn't always have 28 days).
    – Toby Speight
    Nov 12 at 17:48










  • As you seem to be a Stack Overflow user, it's worth reading A guide to Code Review for Stack Overflow users. It will tell you what's expected here that's different to what you're used to.
    – Toby Speight
    Nov 12 at 17:50






  • 2




    Thanks for the intro. I have completed the code. Some constructive feedback around the problem I'm trying to solve would be helpful.
    – benjaminhull
    Nov 13 at 9:23













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have used Enum objects before in PHP as shown here.



However, I often find it's a requirement to have further attributes or metadata available on the instances.



I came up with the example solution below:



<?php

class Month {
const January = 1;
const February = 2;
const March = 3;
const April = 4;
const May = 5;
const June = 6;
const July = 7;
const August = 8;
const September = 9;
const October = 10;
const November = 11;
const December = 12;

protected $value;

protected static $metadata = [
1 => ['days_in_month' => 31],
2 => ['days_in_month' => 28],
3 => ['days_in_month' => 31],
4 => ['days_in_month' => 30],
5 => ['days_in_month' => 31],
6 => ['days_in_month' => 30],
7 => ['days_in_month' => 31],
8 => ['days_in_month' => 31],
9 => ['days_in_month' => 30],
10 => ['days_in_month' => 31],
11 => ['days_in_month' => 30],
12 => ['days_in_month' => 31]
];

public function __construct($value)
{
$this->value = $value;
}

public function __get($name)
{
if (array_key_exists($name, static::$metadata[$this->value])) {
return static::$metadata[$this->value][$name];
}
}
}

$month = new Month(Month::February); // Returns Month instance
$month->days_in_month; // Returns 28 (for February)


Obviously the metadata array can be expanded to include further attributes that should be available for each enumerable option. And of course this need to throw exceptions / handle problems.



Feedback appreciated. Is there a better way to have an Enumerable-type approach which can include additional attributes?










share|improve this question















I have used Enum objects before in PHP as shown here.



However, I often find it's a requirement to have further attributes or metadata available on the instances.



I came up with the example solution below:



<?php

class Month {
const January = 1;
const February = 2;
const March = 3;
const April = 4;
const May = 5;
const June = 6;
const July = 7;
const August = 8;
const September = 9;
const October = 10;
const November = 11;
const December = 12;

protected $value;

protected static $metadata = [
1 => ['days_in_month' => 31],
2 => ['days_in_month' => 28],
3 => ['days_in_month' => 31],
4 => ['days_in_month' => 30],
5 => ['days_in_month' => 31],
6 => ['days_in_month' => 30],
7 => ['days_in_month' => 31],
8 => ['days_in_month' => 31],
9 => ['days_in_month' => 30],
10 => ['days_in_month' => 31],
11 => ['days_in_month' => 30],
12 => ['days_in_month' => 31]
];

public function __construct($value)
{
$this->value = $value;
}

public function __get($name)
{
if (array_key_exists($name, static::$metadata[$this->value])) {
return static::$metadata[$this->value][$name];
}
}
}

$month = new Month(Month::February); // Returns Month instance
$month->days_in_month; // Returns 28 (for February)


Obviously the metadata array can be expanded to include further attributes that should be available for each enumerable option. And of course this need to throw exceptions / handle problems.



Feedback appreciated. Is there a better way to have an Enumerable-type approach which can include additional attributes?







php enum






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 9:22

























asked Nov 12 at 16:54









benjaminhull

61




61












  • What are you using this for, where April to December aren't needed?
    – Toby Speight
    Nov 12 at 17:32










  • I used three months to keep the script short and easy to read - it's an example, you can use imagination to fill out the rest ;)
    – benjaminhull
    Nov 12 at 17:44










  • There is a serious point hidden in my tongue-in-cheek remark - here on Code Review, we prefer complete code where we don't have to imagine what you wrote. (That said, it's good that you include February, which doesn't always have 28 days).
    – Toby Speight
    Nov 12 at 17:48










  • As you seem to be a Stack Overflow user, it's worth reading A guide to Code Review for Stack Overflow users. It will tell you what's expected here that's different to what you're used to.
    – Toby Speight
    Nov 12 at 17:50






  • 2




    Thanks for the intro. I have completed the code. Some constructive feedback around the problem I'm trying to solve would be helpful.
    – benjaminhull
    Nov 13 at 9:23


















  • What are you using this for, where April to December aren't needed?
    – Toby Speight
    Nov 12 at 17:32










  • I used three months to keep the script short and easy to read - it's an example, you can use imagination to fill out the rest ;)
    – benjaminhull
    Nov 12 at 17:44










  • There is a serious point hidden in my tongue-in-cheek remark - here on Code Review, we prefer complete code where we don't have to imagine what you wrote. (That said, it's good that you include February, which doesn't always have 28 days).
    – Toby Speight
    Nov 12 at 17:48










  • As you seem to be a Stack Overflow user, it's worth reading A guide to Code Review for Stack Overflow users. It will tell you what's expected here that's different to what you're used to.
    – Toby Speight
    Nov 12 at 17:50






  • 2




    Thanks for the intro. I have completed the code. Some constructive feedback around the problem I'm trying to solve would be helpful.
    – benjaminhull
    Nov 13 at 9:23
















What are you using this for, where April to December aren't needed?
– Toby Speight
Nov 12 at 17:32




What are you using this for, where April to December aren't needed?
– Toby Speight
Nov 12 at 17:32












I used three months to keep the script short and easy to read - it's an example, you can use imagination to fill out the rest ;)
– benjaminhull
Nov 12 at 17:44




I used three months to keep the script short and easy to read - it's an example, you can use imagination to fill out the rest ;)
– benjaminhull
Nov 12 at 17:44












There is a serious point hidden in my tongue-in-cheek remark - here on Code Review, we prefer complete code where we don't have to imagine what you wrote. (That said, it's good that you include February, which doesn't always have 28 days).
– Toby Speight
Nov 12 at 17:48




There is a serious point hidden in my tongue-in-cheek remark - here on Code Review, we prefer complete code where we don't have to imagine what you wrote. (That said, it's good that you include February, which doesn't always have 28 days).
– Toby Speight
Nov 12 at 17:48












As you seem to be a Stack Overflow user, it's worth reading A guide to Code Review for Stack Overflow users. It will tell you what's expected here that's different to what you're used to.
– Toby Speight
Nov 12 at 17:50




As you seem to be a Stack Overflow user, it's worth reading A guide to Code Review for Stack Overflow users. It will tell you what's expected here that's different to what you're used to.
– Toby Speight
Nov 12 at 17:50




2




2




Thanks for the intro. I have completed the code. Some constructive feedback around the problem I'm trying to solve would be helpful.
– benjaminhull
Nov 13 at 9:23




Thanks for the intro. I have completed the code. Some constructive feedback around the problem I'm trying to solve would be helpful.
– benjaminhull
Nov 13 at 9:23










1 Answer
1






active

oldest

votes

















up vote
0
down vote













February doesn't necessarily have 28 days. In some years, it has 29 days. This code produces the wrong answer for around ¼ of Februaries.






share|improve this answer





















    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207490%2fadding-additional-attributes-to-an-enum-object%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    February doesn't necessarily have 28 days. In some years, it has 29 days. This code produces the wrong answer for around ¼ of Februaries.






    share|improve this answer

























      up vote
      0
      down vote













      February doesn't necessarily have 28 days. In some years, it has 29 days. This code produces the wrong answer for around ¼ of Februaries.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        February doesn't necessarily have 28 days. In some years, it has 29 days. This code produces the wrong answer for around ¼ of Februaries.






        share|improve this answer












        February doesn't necessarily have 28 days. In some years, it has 29 days. This code produces the wrong answer for around ¼ of Februaries.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        Toby Speight

        22.4k537109




        22.4k537109






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207490%2fadding-additional-attributes-to-an-enum-object%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Quarter-circle Tiles

            build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

            Mont Emei