Am 22.02.2012 22:22, schrieb Ángel González:
On 22/02/12 09:37, Sebastian Krebs wrote:
class MyEnum {
     const FOO = 'foo';
     const BAR = 'bar';
     private $value;
     public function __construct ($value) {
         if (!in_array($value, array(self::FOO, self::BAR)) throw new
UnexpectedValueException;
         $this->value = $value;
     }
     public function __toString () { return $this->value; }
}


function doSomething (MyEnum $foo) { /* code */ }


What I wanted to say: I don't see, what is not possible already?

I want to call it doSomething(FOO) or doSomething(MyEnum::FOO),
not doSomething(new MyEnum(MyEnum::FOO));




// The class file
class MyEnum {
     public static $FOO;
     public static $BAR;
     const FOO = 'foo';
     const BAR = 'bar';
     private $value;
     public function __construct ($value) {
if (!in_array($value, array(self::FOO, self::BAR)) throw new UnexpectedValueException;
         $this->value = $value;
     }
     public function __toString () { return $this->value; }
}
/* static code block */ {
  // As long as PHP doesn't support "constant references" (;))
  // (--> constants with objects or arrays)
  MyEnum::$FOO = new MyEnum(MyEnum::FOO);
  MyEnum::$BAR = new MyEnum(MyEnum::BAR);
}
// End class file

// Somewhere deep within some other code
doSomething (MyEnum::$FOO);





However, I can't see, what is the big thing, that isn't currently not possible (except any "I want"-argumentation :X)

Sidenote, according your examples above on how you want call functions: Considered using normal constants?



+1 for adding enums (although I'm open to variations from that exact
proposal).



--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to