0

When I need to pass an enumeration in a php function, I do like :

public function my_function (MyEnum $second) {

  switch ($second->get_value()) {
  case MyEnum::foo:
    ....
  }
  ....
}

class MyEnum {
  const foo = 0;
  const bar = 1;

  private $value;

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

  public function get_value () { return $this->value; }
}

and call like :

my_function(new MyEnum(MyEnum::foo));

Should I DO change my function as to write my_function(MyEnum::foo); ?

I keep writing this way because IDEs show types when auto-completing a function call and php is throwing an Exception if you use another type anyways. Also It is smoothing the documentation writing process.

So I thought this has a lot of advantages right ?

But maybe writing as the latter has others advantages or disadvantages I'm unaware of. Which could be them ?

3
  • What is the point of passing $first to my_function()? Commented Feb 13, 2015 at 9:55
  • @MarkBaker no points, just to look natural, I edited. Commented Feb 13, 2015 at 9:59
  • I don't really get why you use so much code to juste return a constant value, you could juste use: constant('MyEnum::'. $const);. It will return the value of the given $const (so you don't need to use a switch) Commented Feb 13, 2015 at 10:11

2 Answers 2

1

When creating a new MyEnum like so

my_function(new MyEnum(MyEnum::foo));

your IDE still won't be able catch the problem arising when you supply a non-existant value into the constructor:

my_function(new MyEnum(12345));

Though the IDE does not care, your code may not be so happy.

You can keep IDE code completion, warnings and have improved safety by implementing your enums as a class hierarchy instead:

abstract class MyEnum
{
    const foo = 1;
    const bar = 2;

    protected $value;

    public function get_value()
    {
        return $this->value;
    }
}

class Foo extends MyEnum { protected $value = MyEnum::foo; }
class Bar extends MyEnum { protected $value = MyEnum::bar; }

function my_function (MyEnum $second)
{
    switch ($second->get_value()) {
        case MyEnum::foo:
            return 'foo';
            break;
        case MyEnum::bar:
            return 'bar';
            break;
        default:
            // This won't happen
            break;
    }
}

echo my_function(new Foo), PHP_EOL;
echo my_function(new Bar), PHP_EOL;

Output:

foo
bar
Sign up to request clarification or add additional context in comments.

1 Comment

Well, at least it is one way to do it. I have seen it around and I use it quite often myself. Enums have been discussed earlier on this site, like here and all implementations have its pros and cons.
0

PHP 8.1 has introduced enums, so now it's possible to create one of those instead of a class with constants.

Your enum would look like this:

enum MyEnum: int
{
    case foo = 0;
    case bar = 1;
}

You could then use it like this:

my_function(MyEnum::foo);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.