Req #52583 [Com]: Improved casting and hinting, new magic method __cast()
| From: | thomas dot sahlin at invented dot se | Date: | Mon, 02 Sep 2013 12:22:04 +0000 |
| Subject: | Req #52583 [Com]: Improved casting and hinting, new magic method __cast() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-181397@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=52583&edit=1
ID: 52583
Comment by: thomas dot sahlin at invented dot se
Reported by: martin dot leucht at gmail dot com
Summary: Improved casting and hinting, new magic method
__cast()
Status: Open
Type: Feature/Change Request
Package: Class/Object related
Operating System: Irrelevant
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
I think solution 2 would be better since that would also allow caching objects:
public static function __cast($id)
{
if (!isset(self::$cache[$id]))
self::$cache[$id] = new self($id);
return self::$cache[$id];
}
Previous Comments:
------------------------------------------------------------------------
[2013-05-03 19:16:50] mkutschker-php at yahoo dot com
If we have both casting (the other one being #46128) then perhaps the names should be __castFrom()
and castTo(). Or perhaps even __castFromInt() and castToInt() and so on.
------------------------------------------------------------------------
[2012-09-05 02:53:14] aharvey@php.net
Not a dupe: request #46128 covers casting the other way. (That alone is probably a good argument for
changing the proposed magic method name.)
------------------------------------------------------------------------
[2012-09-04 14:20:42] qfox at ya dot ru
dup?: https://bugs.php.net/bug.php?id=46128&thanks=6
------------------------------------------------------------------------
[2010-10-19 11:41:50] rayro at gmx dot de
pretty nice, awesome feature!
but i have some recommendations on this:
1) is it true that i cannot cast to a boolean false? what about throwing the error if no return was
made? the user is able to throw some exception or will simple not return anything to use the default
error mechanism...
2) i think there should be 2 additional types of functions covering your 2 solutions by simply
change the name. for your case 1 this should be __cast, whereas 2 could be __hint? the essential
difference is that __hint will be called in the functions arguments and __cast will be called
elsewhere?
------------------------------------------------------------------------
[2010-10-03 18:45:29] + at ni-po dot com
@degeberg: Doesn't the RFC cover casting the other way round? I.e. Class -> Scalar
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=52583
--
Edit this bug report at https://bugs.php.net/bug.php?id=52583&edit=1