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: 2020-11-03 17:32 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: Suspended Package: Class/Object related
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: martin dot leucht at gmail dot com
New email:
PHP Version: OS:

 

 [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)

Pull Requests

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];
}
 [2020-11-03 17:32 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2020-11-03 17:32 UTC] cmb@php.net
This feature would need discussion for which this bug tracker is
not suitable.  If anybody is still interested in this, please
forward the request to the internals mailing list[1].  For the
time being, I'm suspending this ticket.

[1] <https://www.php.net/mailing-lists.php#internals>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 10 15:01:28 2024 UTC