From:             
Operating system: Any
PHP version:      5.3.3
Package:          Class/Object related
Bug Type:         Bug
Bug description:Mathematical operations convert objects to integers

Description:
------------
When an object defined by an extension, such as SimpleXML, is used in a
mathematical context, it is always converted to an int, never a float. This
is inconsistent with the behaviour of other data types, which are
automatically treated as a float if appropriate [e.g. the string "2.5" used
in mathematical context is not truncated to int(2)].



This is impossible with user-defined classes, since no magic methods are
available for governing cast to float or int, but binary object definitions
can control this behaviour. This results in highly undesirable behaviour
with SimpleXML objects, as reported in #42780



I have searched previous bug reports and the documentation, but cannot see
any justification or documentation regarding this behaviour. In particular,
there is no mention at
http://php.net/manual/en/language.types.type-juggling.php or
http://www.php.net/manual/en/language.types.object.php#language.types.object.casting
and the tables at http://www.php.net/manual/en/types.comparisons.php do not
cover implicit casts of this type.

Test script:
---------------
<?php



$x = simplexml_load_string('<x>2.5</x>');

var_dump($x*1); // int(2)

// type of other operand is irrelevant

var_dump($x*1.0); // float(2)



// strings behave differently

$y = '2.5';

var_dump($y*1); // float(2.5)

var_dump((string)$x*1); // float(2.5)



?>

Expected result:
----------------
All 4 examples should result in float(2.5)

Actual result:
--------------
As commented above. Unless the object is cast to string first, it is
processed as an int, not a float, and the .5 is discarded.

-- 
Edit bug report at http://bugs.php.net/bug.php?id=53033&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=53033&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=53033&r=trysnapshot53
Try a snapshot (trunk):              
http://bugs.php.net/fix.php?id=53033&r=trysnapshottrunk
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=53033&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=53033&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=53033&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=53033&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=53033&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=53033&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=53033&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=53033&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=53033&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=53033&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=53033&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=53033&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=53033&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=53033&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=53033&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=53033&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=53033&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=53033&r=mysqlcfg

Reply via email to