Hi all,
I really missed enums in PHP,
So after reading I tryed to synthetise the various views.
You can find a draft here :
https://github.com/SamNrique/php-src/wiki/RFC-draft
(I can't write on the wiki, and perhaps it's better to finish the
discussion first)
There's an implementation with this draft.
I'd love to have feedbacks because it's my first php-core hack.
Thanks
Samuel Déal
samuel.deal@gmail.com
Hi all,
I really missed enums in PHP,
Why? we have class constant.
thanks
So after reading I tryed to synthetise the various views.
You can find a draft here :
https://github.com/SamNrique/php-src/wiki/RFC-draft
(I can't write on the wiki, and perhaps it's better to finish the
discussion first)There's an implementation with this draft.
I'd love to have feedbacks because it's my first php-core hack.Thanks
Samuel Déal
samuel.deal@gmail.com
--
Laruence Xinchen Hui
http://www.laruence.com/
Right, but enums could possibly be a lot richer than class constants are
now.
They could be a type where the only values are what's defined in the enum.
This could be used with type hinting:
enum Foo {
A,
B,
C
}
function bar(Foo $x) {
// ...
}
There'd be no need to do any manual checking that $x is a valid value.
Perhaps enum values could also be casted back to strings:
(string)Foo::B; // "B"
Sure, this is the kind of stuff that's possible in other ways already, but
first class support always helps.
On Wed, Feb 22, 2012 at 12:45 PM, Samuel Deal samuel.deal@gmail.com
wrote:Hi all,
I really missed enums in PHP,
Why? we have class constant.thanks
So after reading I tryed to synthetise the various views.
You can find a draft here :
https://github.com/SamNrique/php-src/wiki/RFC-draft
(I can't write on the wiki, and perhaps it's better to finish the
discussion first)There's an implementation with this draft.
I'd love to have feedbacks because it's my first php-core hack.Thanks
Samuel Déal
samuel.deal@gmail.com--
Laruence Xinchen Hui
http://www.laruence.com/
2012/2/22 Charlie Somerville charlie@charliesomerville.com
Right, but enums could possibly be a lot richer than class constants are
now.They could be a type where the only values are what's defined in the enum.
This could be used with type hinting:enum Foo {
A,
B,
C
}function bar(Foo $x) {
// ...
}
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?
There'd be no need to do any manual checking that $x is a valid value.
Perhaps enum values could also be casted back to strings:
(string)Foo::B; // "B"
Sure, this is the kind of stuff that's possible in other ways already, but
first class support always helps.On Wed, Feb 22, 2012 at 12:45 PM, Samuel Deal samuel.deal@gmail.com
wrote:Hi all,
I really missed enums in PHP,
Why? we have class constant.thanks
So after reading I tryed to synthetise the various views.
You can find a draft here :
https://github.com/SamNrique/php-src/wiki/RFC-draft
(I can't write on the wiki, and perhaps it's better to finish the
discussion first)There's an implementation with this draft.
I'd love to have feedbacks because it's my first php-core hack.Thanks
Samuel Déal
samuel.deal@gmail.com--
Laruence Xinchen Hui
http://www.laruence.com/
My point is that although it's certainly possible to do almost everything I want already, I should not have to write all that boilerplate for each enum.
I would much prefer to write something like 'enum { A, B, C }' rather than write a full class for the enum, duplicate each enum value multiple times (your example repeats 'foo' and 'bar' three times each), overload the __toString() magic method, do manual error handling in the constructor, etc.
Since PHP provides no way to overload the equality operator, any solution that tries to emulate enums as Samuel or I have written about will be flawed.
2012/2/22 Charlie Somerville <charlie@charliesomerville.com (mailto:charlie@charliesomerville.com)>
Right, but enums could possibly be a lot richer than class constants are
now.They could be a type where the only values are what's defined in the enum.
This could be used with type hinting:enum Foo {
A,
B,
C
}function bar(Foo $x) {
// ...
}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?
There'd be no need to do any manual checking that $x is a valid value.
Perhaps enum values could also be casted back to strings:
(string)Foo::B; // "B"
Sure, this is the kind of stuff that's possible in other ways already, but
first class support always helps.On Wed, Feb 22, 2012 at 12:45 PM, Samuel Deal <samuel.deal@gmail.com (mailto:samuel.deal@gmail.com)>
wrote:Hi all,
I really missed enums in PHP,
Why? we have class constant.thanks
So after reading I tryed to synthetise the various views.
You can find a draft here :
https://github.com/SamNrique/php-src/wiki/RFC-draft
(I can't write on the wiki, and perhaps it's better to finish the
discussion first)There's an implementation with this draft.
I'd love to have feedbacks because it's my first php-core hack.Thanks
Samuel Déal
samuel.deal@gmail.com (mailto:samuel.deal@gmail.com)--
Laruence Xinchen Hui
http://www.laruence.com/
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));
+1 for adding enums (although I'm open to variations from that exact
proposal).
What about SplEnum? (http://php.net/splenum)
It's part of the SPL types extension, but it could probably be also
moved to core.
Nikita
Hi all,
I really missed enums in PHP,
So after reading I tryed to synthetise the various views.You can find a draft here :
https://github.com/SamNrique/php-src/wiki/RFC-draft
(I can't write on the wiki, and perhaps it's better to finish the
discussion first)There's an implementation with this draft.
I'd love to have feedbacks because it's my first php-core hack.Thanks
Samuel Déal
samuel.deal@gmail.com
I think that your proposal looks good, but I'd like to suggest a few
changes. First of all, I'd like to see the association with integers
removed. An enum instance shouldn't just be a name for an integer, it
should be more like a singleton instance of a special kind of class
that can only ever have a predefined set of named instances. There
should be no value associated with it.
Secondly, there already exists the capacity for arbitrary classes to
be used in foreach and count you just have to implement Iterator and
Countable. Other interfaces that all enums might want to implement
automatically are ArrayAccess and Serializable. This would just
require the enum itself to be an object (maybe of type enum or
something) such that it can implement the relevant methods.
http://www.php.net/manual/en/class.iterator.php
http://php.net/manual/en/class.countable.php
Hi all,
I really missed enums in PHP,
So after reading I tryed to synthetise the various views.You can find a draft here :
https://github.com/SamNrique/php-src/wiki/RFC-draft
(I can't write on the wiki, and perhaps it's better to finish the
discussion first)There's an implementation with this draft.
I'd love to have feedbacks because it's my first php-core hack.Thanks
Samuel Déal
samuel.deal@gmail.com
Hello, i should point out that it is all unicorns and rainbows for building
inernal apis and libraries. But in PHP you work with the world out there.
And that means you process GET, POST, xml, json and bunch of other data,
witch comes to you as text. Even db requests usually retirn text even with
numeric data. It all works great and easy now because PHP handles the
conversion, just have to check if the data is correct. So what will happen
with strict typing if i define an int and external data gives me a numeric
string? Or some ass tries to send me an array. Code just breaks into fatal
error? You can build a safe code that handles that, but if you write jist a
bunch of pretty strait forward scripts and you don't need all that complex
code with a framework type handling, sanitizing and so on.
Sure, inernal apis would be strictier and so on, but hell even now i think
it will make so much pain to make sure to convert all the data and params -
every db row, get params, xml data, etc. Duck typing is what makes php a
great tool for web development. And type hinting should be weak, trying to
convert data if is a now brainer and fail on real mistakes, like now
honting that you expect an array and a string is passed. But expecting an
int and passing a string containing int number should go smooth
From: Arvids Godjuks [mailto:arvids.godjuks@gmail.com]
Hello, i should point out that it is all unicorns and rainbows for building inernal apis and libraries. But in PHP you work with the world out there.
And that means you process GET, POST, xml, json and bunch of other data, witch comes to you as text. Even db requests usually retirn text even with numeric data. It all works great and easy now because PHP handles the conversion, just have to check if the data is correct. So what will happen with strict typing if i define an int and external data gives me a numeric string? Or some ass tries to send me an array. Code just breaks into fatal error? You can build a safe code that handles that, but if you write jist a bunch of pretty strait forward scripts and you don't need all that complex code with a framework type handling, sanitizing and so on.Sure, inernal apis would be strictier and so on, but hell even now i think it will make so much pain to make sure to convert all the data and params - every db row, get params, xml data, etc. Duck typing is what makes php a great tool for web development. And type hinting should be weak, trying to convert data if is a now brainer and fail on real mistakes, like now honting that you expect an array and a string is passed. But expecting an int and passing a string containing int number should go smooth
Nobody really wants typing as strict as what you are implying above. Even C++ (some of the strictest typing available) does implicit conversions where appropriate. All of the following should be perfectly valid:
(function(integer $n){})('1');
(function(string $s){})(1);
(function(float $f){})(1);
(function(integer $n){})(1.0);
(function(string $s){})(1);
(function(boolean $b){})($foo);
(function(string $s){})($some_object_with_a___tostring_magic_method);
As far as unconvertible inputs, here's the thing; most logic needs to know roughly what it is working with and only works with a limited range of valid inputs. For example, consider substr()
, which will never make any sense unless you pass a string and an integer. If you try to call substr('foo', 'bar') it's going to have to fail, the only question is at what level and how. So called "strict typing" doesn't cause new errors, it gives structure to a ubiquitous set of existing error conditions.
John Crenshaw
Priacta, Inc.