0

It is possible to create sets of enumeration types in PHP like in Delphi?

I want to create a set of options for a component like [required, readonly, ...]. I've seen PHP and Enumerations but it does not solve my problem completely.

For intance, in Delhpi you can do something like:

type enum_options = (required, readonly, ...); // Enumerated type
options_for_my_component := [required, readonly];
...
if (required in options_for_my_component) then
   ...

More info at http://www.delphibasics.co.uk/Article.asp?Name=Sets

jciberta
  • 31
  • 1
  • 7
  • 1
    "but it does not solve my problem completely" <- how so? – Jeto Mar 29 '19 at 08:18
  • @Jeto: According to the example at https://stackoverflow.com/questions/254514/php-and-enumerations I cannot see how to do, for instance: `working_days = [Monday, Tuesday, ..];` (this is a set of enumeration) and then `if (today in working_days) then ...`. Notice I'm using Pascal style – jciberta Mar 31 '19 at 09:00

1 Answers1

0

Still a bit unsure what you're missing from PHP here. Using in_array it should be pretty straightforward (unless I missed something):

abstract class DaysOfWeek
{
  const Sunday = 0;
  const Monday = 1;
  const Tuesday = 2;
  const Wednesday = 3;
  const Thursday = 4;
  const Friday = 5;
  const Saturday = 6;
}

$working_days = [DaysOfWeek::Monday, DaysOfWeek::Tuesday, DaysOfWeek::Wednesday, DaysOfWeek::Thursday, DaysOfWeek::Friday];
$today = (int)date('w');

echo in_array($today, $working_days) ? 'Today is a working day!' : 'Today is a weekend day!';

Demo: https://3v4l.org/RJg9B

(You can try this demo tomorrow and it should say it's a working day :))

Jeto
  • 14,596
  • 2
  • 32
  • 46