php.net |  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
Votes:7
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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
33 + 31 = ?
Subscribe to this entry?

 
 [2008-11-12 22:47 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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

[1] <https://wiki.php.net/rfc/howto>
 [2020-12-23 17:16 UTC] cmb@php.net
-Status: Feedback +Status: Suspended -Assigned To: cmb +Assigned To:
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 15:01:29 2024 UTC