Edit report at https://bugs.php.net/bug.php?id=52583&edit=1
ID: 52583
Updated by: aharvey@php.net
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:
Not a dupe: request #46128 covers casting the other way. (That alone is probably a good argument for
changing the proposed magic method name.)
Previous Comments:
------------------------------------------------------------------------
[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
------------------------------------------------------------------------
[2010-08-11 16:54:01] degeberg@php.net
See: http://wiki.php.net/rfc/class_casting_to_scalar
------------------------------------------------------------------------
[2010-08-11 16:30:02] martin dot leucht at gmail dot com
Description:
------------
Type casting in PHP is currently limited to the builtin types. It would be very useful (IMO) if one
could cast a value/variable to a class. And also type hinting could be improved by casting instead
of type checking only.
I suggest to solve this by introducing a new magic method for classes, called "__cast()",
that accepts the value to cast as parameter.
I see two possible solutions for its functionality:
(1) __cast() replaces the constructor method
This will force the developer to move logics from the constructor into a separate function/method
(which isn't even so bad) or to copy his code (which is indeed bad). If the function returns
with the boolean value "false", the default error mechanism is started saying something
like "cast to XYZ not allowed".
(2) __cast() acts like a static constructor
The code must return the casted result itself. That means one would implement some logics to
transform the given value to the needed constructor parameters and create a new instance on his own.
To handle an unsupported value the method would throw an exception or an error. The negative (or
let's say curious) thing about this solution is, that this cast method could also return a
value of a totally different type (see example).
Test script:
---------------
// see patch file
Expected result:
----------------
// working cast
Actual result:
--------------
// parse errors
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=52583&edit=1