php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48076 Magic method __toNumber
Submitted: 2009-04-25 14:21 UTC Modified: 2009-04-27 08:39 UTC
Votes:5
Avg. Score:4.4 ± 0.8
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: av3ng3r at gmail dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.3.0RC1 OS: Doesn't matter
Private report: No CVE-ID: None
 [2009-04-25 14:21 UTC] av3ng3r at gmail dot com
Description:
------------
Add the magic method __toNumber.

Reproduce code:
---------------
class Number
{
   private $number;
 
   //some useful example methods
   function isBetween($a, $b) { //is number between ? }
   //etc

   function __toNumber()
   {
     return $this->number;
   }
}

Expected result:
----------------
$c = new Number(3);

echo $c; //outputs: 3
var_dump($c->isBetween(2, 4)) //returns true

//cast it too a Price object (extended from Number)
$price = (Price) $c;

//override __toNumber()
echo $c; //outputs 3.00 for example


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-04-27 08:39 UTC] johannes@php.net
This has been discussed and rejected multiple times as we'd have some conflicts. For instace should $a = new $object(); $b = array(); $b[$a] = 1; call toString or toNumber?

Please see other bug reports on this and internals archives for more reasons.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 15:01:30 2024 UTC