Req #46560 [Com]: Addition of magic method "__valueOf" allowing object conversion to number

From: Date: Sun, 22 Dec 2013 22:47:59 +0000
Subject: Req #46560 [Com]: Addition of magic method "__valueOf" allowing object conversion to number
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-183436@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=46560&edit=1 ID: 46560 Comment by: 5863100482fucu at gmail dot com Reported by: jonasraoni at gmail dot com Summary: Addition of magic method "__valueOf" allowing object conversion to number Status: Open Type: Feature/Change Request Package: Feature/Change Request PHP Version: 5.3.0alpha2 Block user comment: N Private report: N New Comment: WTF!! IM JUST WANTING TO KNOW WHY ME!! IM NOT RICH! IM JUST ME STRUGGLING WITH NO FAMILY TRYING TO EXPERIMENT LIFE AROUND THE WORLD ON THE WEB AND INTERNET!! OH MY GOD!!! PLEASE LEAVE ME ALONE! IVE ALREADY HAD A NERVOUS BREAKDOWN CUZ OF EVERYTHING THATS HAPPEN TO ME HERE!! WHOM EVER YOU ARE LET ME HAVE A GREAT TIME HERE ON THE SITES! WITHOUT HAVING TO WORRY ABOUT GOING TO JAIL OVER WHAT SOMEONE ELSE HAS DID TO MY REPUTATION! THANKS... Previous Comments: ------------------------------------------------------------------------ [2010-05-07 12:59:11] dbforch at hotmail dot com I totally agree. The SplType class (http://pecl.php.net/package/SPL_Types) is the only class I know of in PHP5 that is able to convert an Object to a number (I only manage to install it under PHP 5.2.10-2ubuntu6.4, all PHP 5.3.X versions were a no-go on make-errors). Code: ----- $i1 = new SplInt(11); $i2 = new SplInt(11); echo $i1+$i2; Result: ------- 22 The SplType also doesn't have a method (protected) to change the internal value when extending the SplInt with a custom defined class. Different values for $i1 and $i2 after initializing can only be set by unsetting it {unset($i1);$i1=new SplFloat(0.3)}. This makes it unusable in every way. The SplType::__constructor($initial_value, (bool) $strict) comes with a $strict value, this not only hides the warnings, it also does not change the values of the assigned variable as it should be. Code: ----- $i = new SplInt(12, $strict = false); $i = new SplFloat(10.244); echo $i; Expected Result: 10.244 Result: 10 The only possible solution I could think of to represent an Object as a number. Code: ----- class base { public function __toString() { return "11"; } // requested feature not used in the first example, but in the second public function __valueOf() { return 11; } } $c1 = new base; $c2 = new base; echo (string)$c1+(string)$c2; Result: ------- 22 By adding the requested magic function from 'jonasraoni at gmail dot com', you could produce the following code. Additional Code: ---------------- echo $c1.' + '.$c2.' = '.($c1+$c2); Result: ------- 11 + 11 = 22 Choices can be made in which situation to use the __toString and the __valueOf (or whatever name it should have). Second, representing an object as a number isn't that difficult. Looking at the source of SPL_Types, it really isn't that magical. ------------------------------------------------------------------------ [2008-11-12 22:47:54] jonasraoni at gmail dot com Description: ------------ In the current implementation it's impossible to convert an object to a number. I should exist a magic method called "__valueOf" which must return a float/int. In the case this magic method isn't declared , it should be returned a not zero value when converting to number (it happens, but generates the E_NOTICE anyway). Reproduce code: --------------- class Number{ private $value; public function __construct($n){ $this->value = +$n; } public function __toString(){ return (string)$this->value; } public function __valueOf(){ return $this->value; } } $n = new Number(2); echo $n . ':' . +$n; Expected result: ---------------- 2:2 Actual result: -------------- Notice: Object of class Number could not be converted to int in C:\test.php on line 16 2:1 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=46560&edit=1

« previous php.bugs (#183436) next »