Edit report at http://bugs.php.net/bug.php?id=46560&edit=1
ID: 46560
Comment by: dbforch at hotmail 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
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[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 http://bugs.php.net/bug.php?id=46560&edit=1