php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65893 cast to int/float always returns 1 for objects
Submitted: 2013-10-14 11:47 UTC Modified: 2013-10-14 13:33 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: worldoffame at hotmail dot com Assigned:
Status: Wont fix Package: Class/Object related
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: worldoffame at hotmail dot com
New email:
PHP Version: OS:

 

 [2013-10-14 11:47 UTC] worldoffame at hotmail dot com
Description:
------------
When I create an object with integer/float representation, Id expect that (int) and (float) casting will first convert the object using __toString(). This results in a numeric string, and they can then be cast into integers/floats. This does not seem to work at all as __toString() is not called when you cast objects to int/float. So instead I always get the number 1, a very annoying glitch I must say. 

Test script:
---------------
// A number class
abstract class Number extends Object{
    protected $value;

    public function __toString(){
        return (string)$this->value;
    }
}

class Integer extends Number{
    public function __construct($value){
        $this->value = (int)$value;
    }
}

class Float extends Number{
    public function __construct($value){
        $this->value = (float)$value;
    }
}

// Create integer and float objects
$int = new Integer(3);
$float = new Float(2.5);

var_dump((int)$int);
var_dump((float)$float);

Expected result:
----------------
int(3)
float(2.5)

Actual result:
--------------
int(1)
float(1)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-14 13:23 UTC] phpmpan at mpan dot pl
`__toString` serves mainly debugging purpose when one needs to output an object to the log easily or lookup its value fast during development. What's the purpose of casting an object to a number?
 [2013-10-14 13:25 UTC] worldoffame at hotmail dot com
There are lots of points, in my script everything is an object and each field in domain model object uses integer/float/string objects rather than primitives. The fact that you dont see how useful it can be doesnt mean its not useful for others.
 [2013-10-14 13:33 UTC] johannes@php.net
-Status: Open +Status: Wont fix
 [2013-10-14 13:33 UTC] johannes@php.net
Adding support for these casts would lead to quite some complication, making it harder to debug scripts. We understand that there are uses cases but have decided against.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Aug 05 19:01:23 2021 UTC