|  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
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
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-10-14 11:47 UTC] worldoffame at hotmail dot com
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);


Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


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]
-Status: Open +Status: Wont fix
 [2013-10-14 13:33 UTC]
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: Sat Jul 24 12:01:23 2021 UTC