Bug #65893 [Opn->Wfx]: cast to int/float always returns 1 for objects

From: Date: Mon, 14 Oct 2013 13:33:52 +0000
Subject: Bug #65893 [Opn->Wfx]: cast to int/float always returns 1 for objects
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-182149@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=65893&edit=1

 ID:                 65893
 Updated by:         johannes@php.net
 Reported by:        worldoffame at hotmail dot com
 Summary:            cast to int/float always returns 1 for objects
-Status:             Open
+Status:             Wont fix
 Type:               Bug
 Package:            Class/Object related
 Operating System:   Irrelevant
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2013-10-14 13:25:57] 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:23:51] 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 11:47:00] 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)


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=65893&edit=1


Thread (4 messages)

« previous php.bugs (#182149) next »