|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #46560 Addition of magic method "__valueOf" allowing object conversion to number
Submitted: 2008-11-12 22:47 UTC Modified: 2020-12-23 17:16 UTC
Avg. Score:4.7 ± 0.7
Reproduced:7 of 7 (100.0%)
Same Version:0 (0.0%)
Same OS:3 (42.9%)
From: jonasraoni at gmail dot com Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: 5.3.0alpha2 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-11-12 22:47 UTC] jonasraoni at gmail dot com
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:

Actual result:
Notice: Object of class Number could not be converted to int in C:\test.php on line 16


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-07 12:59 UTC] dbforch at hotmail dot com
I totally agree.

The SplType class ( 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).

$i1 = new SplInt(11);
$i2 = new SplInt(11);
echo $i1+$i2;


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.

$i = new SplInt(12, $strict = false);
$i = new SplFloat(10.244);
echo $i;

Expected Result:


The only possible solution I could think of to represent an Object as a number.

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;


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

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.
 [2020-12-23 17:16 UTC]
-Status: Open +Status: Feedback -Package: Feature/Change Request +Package: Scripting Engine problem -Assigned To: +Assigned To: cmb
 [2020-12-23 17:16 UTC]
This feature requires the RFC process[1].  Feel free to start that
process.  For the time being, I'm suspending this ticket.

[1] <>
 [2020-12-23 17:16 UTC]
-Status: Feedback +Status: Suspended -Assigned To: cmb +Assigned To:
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Jan 24 02:01:29 2021 UTC