Re: Object Casting - An Alternative to Type Hinting
| From: | Anthony Ferrara | Date: | Mon, 27 Feb 2012 02:50:56 +0000 |
| Subject: | Re: Object Casting - An Alternative to Type Hinting | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-58113@lists.php.net to get a copy of this message | ||
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 extreme performance penalties, as every callback in
the chain (at least up to a valid cast) would then need to be called
for every single cast operation.
Not to mention that you're now externalizing where the cast happens
into a non-obvious and non-extendable spot (meaning that you're
removing the possibility of modifying behavior via polymorphism)...
I prefer the concept of binding the cast operations (two, for
symmetry) to the class itself, as it localizes the functionality to
that class, and is quite predictable and extendable.
If I missed the point you were trying to make, please elaborate.
Thanks,
Anthony
2012/2/26 Ángel González <keisial@gmail.com>:
> 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 that's the perfect solution, though.
>