Skip to content

Commit b824b11

Browse files
committed
Enums as singletons are not cloneable
1 parent 13532cd commit b824b11

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/MabeEnum/Enum.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ final private function __construct($value)
5252
$this->value = $constants[$const];
5353
}
5454

55+
/**
56+
* Get the selected value
57+
* @return string
58+
* @see getValue()
59+
*/
60+
final public function __toString()
61+
{
62+
return (string) $this->value;
63+
}
64+
65+
/**
66+
* @throws LogicException Enums are not cloneable
67+
* because instances are implemented as singletons
68+
*/
69+
final private function __clone()
70+
{
71+
throw new LogicException('Enums are not cloneable');
72+
}
73+
5574
/**
5675
* Get the current selected value
5776
* @return mixed
@@ -94,16 +113,6 @@ final public function getOrdinal()
94113
return $ordinal;
95114
}
96115

97-
/**
98-
* Get the current selected constant name
99-
* @return string
100-
* @see getName()
101-
*/
102-
final public function __toString()
103-
{
104-
return (string) $this->value;
105-
}
106-
107116
/**
108117
* Get an enum of the given value
109118
*

0 commit comments

Comments
 (0)