Re: Re: Clarification on the Enum language structure

From: Date: Wed, 23 Feb 2011 09:46:34 +0000
Subject: Re: Re: Clarification on the Enum language structure
References: 1 2 3 4 5 6 7 8 9 10 11 12 13  Groups: php.internals 
Request: Send a blank email to internals+get-51507@lists.php.net to get a copy of this message
public function Killjoy(MyEnum $x)
What would be the purpose of such code? What would it do if 5 is passed as $x?
Are you suggesting this as an enum member function, or just a regular function in any old class? If 'in an enum', I think by analogy with the stuff somebody linked to on the MS site to do with C#, it can only ever be invoked by doing EnumConstant.Killjoy(); and EnumConstant is automatically passed as an argument to the function--you could never pass 5. I think this is a bit crazy, though, blurring the lines between enums and classes way too much, and not a good idea for PHP. If just 'any old function', there are two possibilities: if 5 is passed, the type hint recognises that 5 is not of the MyEnum type, so fails; you would have to explicitly cast 5 as a MyEnum when calling the function to make it work; or the other possibility is that Killjoy attempts a cast and only fails if 5 cannot be cast to the enum (I prefer this way). In both cases, though, that would not be expected to be the usual way to use the function: you would usually be expected to pass in a true enum constant of the MyEnum type. Ben.

« previous php.internals (#51507) next »