Bug #65893 [NEW]: cast to int/float always returns 1 for objects

From: Date: Mon, 14 Oct 2013 11:47:00 +0000
Subject: Bug #65893 [NEW]: cast to int/float always returns 1 for objects
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-182144@lists.php.net to get a copy of this message
From:             worldoffame at hotmail dot com
Operating system: Irrelevant
PHP version:      Irrelevant
Package:          Class/Object related
Bug Type:         Bug
Bug description:cast to int/float always returns 1 for objects

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 bug report at https://bugs.php.net/bug.php?id=65893&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=65893&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=65893&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=65893&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=65893&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=65893&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=65893&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=65893&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=65893&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=65893&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=65893&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=65893&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=65893&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=65893&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65893&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=65893&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=65893&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=65893&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=65893&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=65893&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=65893&r=mysqlcfg



Thread (4 messages)

« previous php.bugs (#182144) next »