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

From: Date: Fri, 09 Nov 2012 16:00:11 +0000
Subject: Req #46128 [Com]: Magic function __cast($to)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-174600@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
 Comment by:         shabazzk at gmail dot com
 Reported by:        131 dot php at cloudyks dot org
 Summary:            Magic function __cast($to)
 Status:             Open
 Type:               Feature/Change Request
 Package:            Feature/Change Request
 Operating System:   Linux
 PHP Version:        5.2.6
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[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!

------------------------------------------------------------------------
[2011-01-20 22:54:12] neoegm at hotmail dot com

This would be very useful in order to be able to define a specific evaluation method for an object
as a boolean, to, for example, simplify if-switches, so you can just do:

if ($obj)
{
	//...
}

Here an example of a class which toggles its own value each time it's evaluated as a boolean:

class ToggleObj
{
	var $val = FALSE;

	function __toBool()
	{
		$ret = $this->val;
		$this->val = !$ret;
		return $ret;
	}
}

$obj = new ToggleObj;

if ($obj)
{
	//Does not enter
}

if ($obj)
{
	//Now it does
}


Or going further, to make even table rows highlighted:

foreach ($rows as $row)
{
?>
	<tr class="<?=$obj?"":"highlighted"?>">
		<td><?=$row?></td>
	</tr>
<?php
}

Thanks.

NeoEGM
http://www.neoegm.com/

------------------------------------------------------------------------
[2009-03-28 12:41:30] andrea at 3site dot it

I wonder why you guys do not implement in core the PECL php_operator which would make code style and
life much easier.

I cannot imagine a Number class which needs (int)$obj for every single operation.
Please do not get me wrong, __cast is a good idea, but it covers only explicit cases while every
other decent language (C#, Java, Python) allows developer to implement implic cast behavior as well.

This, in PHP 5.3, would be excellent (but probaly an illusion thou).
Regards

------------------------------------------------------------------------
[2009-02-06 00:11:19] rayro at gmx dot de

This is such a nice Implementation and very useful, but i prefer to add the methods __toBool,
__toInt and __toArray rather than __cast, stay tuned with PHP's other magic methods..

I dont agree fully with that post in feature request #38508 from helly. If that'll be the way,
why did the decision for magic methods?
Isnt that all complex although? ^^
We know, but in my eyes (and many others), i think that these magically stuff is one of the top key
features for php. And none of these features will become critism i think. If not needed, just dont
use them!

The additional goody __toInvoke() introduced in 5_3 is a such nice addition for developing
"quick gets" based on nested object sets:
<?php
$magazine(3)->getArticles();
// old way
$magazine->getArticle($magazine->useMagazine(3));
?>
This helper is a "backdoor" like way to enable PHP to fully write nested Objects like in
Javascript. And my opinion for that: I LIKE THIS :)

I think this is a very very discussable topic to the changes for 5_3 or 6_0 beside the wanted
support for traditional type hinting and utf8!

thanks

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


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 (#174600) next »