Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-28 Thread jpauli
On Tue, Feb 28, 2012 at 6:05 AM, Anthony Ferrara ircmax...@gmail.comwrote: Ok, I've made a proof-of-concept patch here: https://gist.github.com/1929587 Note that there are still a few memory leaks in there that would need to be cleaned up, but I just wanted to make a really quick POC before

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-28 Thread Anthony Ferrara
One could use zend_get_type_by_const() for help :) I wasn't aware of that function, thanks! I based it off of gettype ( http://lxr.php.net/opengrok/xref/PHP_5_4/ext/standard/type.c#26 ). I'll update the patch tonight. And before that, we would need several RFC (one per concept seems to be a

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-28 Thread Richard Lynch
On Mon, February 27, 2012 11:05 pm, Anthony Ferrara wrote: ?php class Foo { public $value = 1; } class Bar { public $value = 1; public function __castTo($type) { return $this-value; } public function __assign($value) { $this-value = $value;

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-28 Thread Anthony Ferrara
Richard, This is not overloading addition. Not by a long shot. This is overloading casting to primitives (which happens in the case of addition, but a bunch of others). It turns out that the addition operator calls object-get in C to get the primitive value of the object if it exists. That's

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-28 Thread Kris Craig
I think this is +1 for moving the conversation to a less crowded location. Sorry guys I know I keep promising to take care of it but I've been swamped all day. I'll try to find some time though. --Kris On Tue, Feb 28, 2012 at 4:28 PM, Anthony Ferrara ircmax...@gmail.comwrote: Richard, This

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-27 Thread Richard Lynch
On Sun, February 26, 2012 9:48 am, Anthony Ferrara wrote: I have to say that no matter how much a luv my OOP, turning every built-in type into an Object is just a Bad Idea... It's a form of bloat on RAM and CPU with minimal added value, imho. No matter which way you twist this pretzel: -1 --

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-27 Thread Richard Lynch
On Sun, February 26, 2012 8:45 pm, Anthony Ferrara wrote: Or operator-overlading to the rescue? :-) Not quite. Especially because with operator overloading done at this level (how it would be implemented in PHP) it's almost impossible to make it consistent: class string { public

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-27 Thread Anthony Ferrara
I have to say that no matter how much a luv my OOP, turning every built-in type into an Object is just a Bad Idea... It's a form of bloat on RAM and CPU with minimal added value, imho. No matter which way you twist this pretzel: -1 Re-read what I had written. I never said to turn every

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-27 Thread Ángel González
On 27/02/12 16:12, Richard Lynch wrote: Oh, and string is a reserved word, so this won't work as-is, though that's obviously picuyane. It's not, you can perfectly define your own class called 'string'. I'd be much easier if it were, though. -- PHP Internals - PHP Runtime Development Mailing

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-27 Thread Richard Lynch
On Mon, February 27, 2012 9:20 am, Anthony Ferrara wrote: I have to say that no matter how much a luv my OOP, turning every built-in type into an Object is just a Bad Idea... It's a form of bloat on RAM and CPU with minimal added value, imho. Re-read what I had written. I never said to turn

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-27 Thread Matthew Weier O'Phinney
On 2012-02-27, Richard Lynch c...@l-i-e.com wrote: On Mon, February 27, 2012 9:20 am, Anthony Ferrara wrote: I have to say that no matter how much a luv my OOP, turning every built-in type into an Object is just a Bad Idea... It's a form of bloat on RAM and CPU with minimal added

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-27 Thread Anthony Ferrara
Rich, I appreciate the candid and honest nature of your reply, while maintaining civility. This list needs more of that. Further replies inline: On Mon, Feb 27, 2012 at 1:54 PM, Richard Lynch c...@l-i-e.com wrote: On Mon, February 27, 2012 9:20 am, Anthony Ferrara wrote: I have to say that no

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-27 Thread Anthony Ferrara
Ok, I've made a proof-of-concept patch here: https://gist.github.com/1929587 Note that there are still a few memory leaks in there that would need to be cleaned up, but I just wanted to make a really quick POC before cleaning anything up to make it worthy of addition... Another patch would need

RE: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-26 Thread Clint M Priest
I definitely like the idea of being able to cast objects, I've frequently wished to be able to cast an object to an array. I would argue against the single __castTo() and __castFrom() magic methods as large switches get to be difficult to find/read and doesn't support separation. I would

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-26 Thread Anthony Ferrara
Clint, First off, thanks for the reply. With respect to the large switches, that could easily be avoided with something like: public function __castTo($type) { $method = 'castTo' . $type; if (method_exists(array($this, $method))) { return $this-$method(); } throw new

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-26 Thread Laruence
On Sun, Feb 26, 2012 at 10:57 PM, Anthony Ferrara ircmax...@gmail.com wrote: I've gone back and re-read a bunch of the old posts on Type Hinting, and have come to the conclusion that it won't be done any time soon. Not because it doesn't have merit, but because there are at least a few

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-26 Thread Anthony Ferrara
I have to say, it doesn't get work, thinking this: $mixed1 = new Interger(2); $mixed2 = new Interge(3); $guess_what_type_is = $mixed1 + $mixed2; thanks That one is actually pretty straight forward. Since `+` is a numeric operation (with the one exception of array + array), it would call

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-26 Thread Gustavo Lopes
On Sun, 26 Feb 2012 16:39:10 +0100, Laruence larue...@php.net wrote: On Sun, Feb 26, 2012 at 10:57 PM, Anthony Ferrara ircmax...@gmail.com wrote: I have to say, it doesn't get work, thinking this: $mixed1 = new Interger(2); $mixed2 = new Interge(3); $guess_what_type_is = $mixed1 + $mixed2;

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-26 Thread Stefan Neufeind
On 02/26/2012 04:48 PM, Anthony Ferrara wrote: I have to say, it doesn't get work, thinking this: $mixed1 = new Interger(2); $mixed2 = new Interge(3); $guess_what_type_is = $mixed1 + $mixed2; thanks That one is actually pretty straight forward. Since `+` is a numeric operation (with

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-26 Thread Ángel González
On 26/02/12 15:57, Anthony Ferrara wrote: I've gone back and re-read a bunch of the old posts on Type Hinting, and have come to the conclusion that it won't be done any time soon. Not because it doesn't have merit, but because there are at least a few fundamental difficulties that are

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-26 Thread Ángel González
I just realised that if it were going to add magic casting, it could as well be done with a spl_autocast_register(), so that you could either cast things when they match, throw an exception, etc. (there should be some default value dynamic typing, so the perfomance wouldn't hurt) . I don't think

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-26 Thread Kris Craig
I like it, at least from a raw conceptual standpoint. I think you might be on to something here, though I'd need to take some time to deliberate on it in more detail. But my initial gut reaction is that this would at very least be a step in the right direction. =) --Kris On Sun, Feb 26, 2012

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-26 Thread Anthony Ferrara
Or operator-overlading to the rescue? :-) Not quite. Especially because with operator overloading done at this level (how it would be implemented in PHP) it's almost impossible to make it consistent: class string { public function overload+($mixed) { return $this-value + $mixed;

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-26 Thread Anthony Ferrara
I fail to see how you would do it via a register function... Unless you mean a call-chain of callbacks to try to cast with a from and a to parameter: spl_autocast_register(function($from, $to) { if ($to == 'Integer') { return new Integer((int) $from); } }); That could have

Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting

2012-02-26 Thread Simon Schick
Hi, This does not seem like a good way for me ... Think about combining many scripts. It's for example quite usual to use Symfony and parts of Zend Framework together. Think about what will happen if the one framework uses another autocast logic ... Bye Simon 2012/2/27 Anthony Ferrara