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?
php enum
add a comment |
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?
php enum
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
add a comment |
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?
php enum
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
php enum
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
February doesn't necessarily have 28 days. In some years, it has 29 days. This code produces the wrong answer for around ¼ of Februaries.
answered 2 days ago
Toby Speight
22.4k537109
22.4k537109
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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