Req #46128 [Opn]: Magic function __cast($to)

From: Date: Wed, 13 Jul 2016 18:15:49 +0000
Subject: Req #46128 [Opn]: Magic function __cast($to)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-202287@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=46128&edit=1

 ID:                 46128
 Updated by:         cmb@php.net
 Reported by:        131 dot php at cloudyks dot org
 Summary:            Magic function __cast($to)
 Status:             Open
 Type:               Feature/Change Request
-Package:            Feature/Change Request
+Package:            *General Issues
 Operating System:   Linux
 PHP Version:        5.2.6
 Block user comment: N
 Private report:     N

 New Comment:

> If we want this feature, then we should take it upon ourselves
> to implement and we should all support it getting in to a
> release version.

Well said, see <https://wiki.php.net/rfc/howto>.


Previous Comments:
------------------------------------------------------------------------
[2013-05-04 15:02:53] shabazzk at gmail dot com

I think we're preaching to the choir at this point. I don't think its a high 
priority to anyone who is actually developing PHP. If we want this feature, then 
we should take it upon ourselves to implement and we should all support it 
getting 
in to a release version.

I'm sure once this is in there then that is when the masses will wonder how they 
ever did without it.

------------------------------------------------------------------------
[2013-05-03 19:10:07] mkutschker-php at yahoo dot com

While __toXxx() may more in line with the existing __toString() IMHO __cast() is more in line with
the other magic functions and gettype(). If I could choose I'd take __cast() and deprectate
__toString().

------------------------------------------------------------------------
[2012-11-09 16:00:11] shabazzk at gmail dot com

My suggestion is to replace __cast($to) with __toObject($object) (or even 
__toSelf($object)). That way, we have a magic method for each type to cast to, 
and we avoid the messy switch/if statements.

------------------------------------------------------------------------
[2012-09-05 21:01:02] maciej dot sz at gmail dot com

I think we all need this badly. It just suits the direction in which the 5.4 is going.

Regarding same functionality I think it is worth nothing that the conversion to boolean should be
implicit. Otherwise such code could not work:

<?php
function check($response) {
  if ( ! $response ) die('error');
}

$resp = new Response();
check($resp->isSuccessful());
check($resp); // this would fail without the cast if the conversion was explicit
?>

------------------------------------------------------------------------
[2012-09-04 14:18:13] qfox at ya dot ru

WHEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEN?!!
There was 3 years to patch sources!

------------------------------------------------------------------------


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=46128


--
Edit this bug report at https://bugs.php.net/bug.php?id=46128&edit=1


Thread (15 messages)

« previous php.bugs (#202287) next »