php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52583 Improved casting and hinting, new magic method __cast()
Submitted: 2010-08-11 16:30 UTC Modified: 2012-09-05 02:53 UTC
Votes:15
Avg. Score:4.7 ± 0.7
Reproduced:14 of 14 (100.0%)
Same Version:7 (50.0%)
Same OS:7 (50.0%)
From: martin dot leucht at gmail dot com Assigned:
Status: Open Package: Class/Object related
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2010-08-11 16:30 UTC] 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

Patches

example-cast-magic-method (last revision 2010-08-11 14:32 UTC by martin dot leucht at googlemail dot com)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-03 18:45 UTC] + at ni-po dot com
@degeberg: Doesn't the RFC cover casting the other way round? I.e. Class -> Scalar
 [2010-10-19 11:41 UTC] 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?
 [2012-09-04 14:20 UTC] qfox at ya dot ru
dup?: https://bugs.php.net/bug.php?id=46128&thanks=6
 [2012-09-05 02:53 UTC] 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.)
 [2013-05-03 19:16 UTC] 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.
 [2013-09-02 12:22 UTC] thomas dot sahlin at invented dot se
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];
}
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Oct 23 16:01:30 2019 UTC