Bug #53033 [Com]: Mathematical operations convert objects to integers
Edit report at https://bugs.php.net/bug.php?id=53033&edit=1
ID: 53033
Comment by: qheaden at phaseshiftsoftware dot com
Reported by: rowan dot collins at gmail dot com
Summary: Mathematical operations convert objects to integers
Status: Verified
Type: Bug
Package: Class/Object related
Operating System: Any
PHP Version: 5.3.3
Block user comment: N
Private report: N
New Comment:
It looks like this issue affects all PHP versions according to the following output: https://3v4l.org/OqOJq
I am willing to check this bug out.
Previous Comments:
------------------------------------------------------------------------
[2010-10-11 06:39:06] cataphract@php.net
read: (...) and PHP prefers a int (not a double).
By the way, the work-around is to ask for a double conversion explicitly:
<?php
$x = simplexml_load_string('<x>2.5</x>');
var_dump((double)$x*1); // float(2.5)
------------------------------------------------------------------------
[2010-10-11 06:37:41] cataphract@php.net
This is tricky because there's no way to tell an object to cast itself to a numeric type
without asking explicitly for an int or a double and PHP prefers a double.
It would be possible to try first a double and, if failing, try an integer, but it's not
straightforward because the current functions/macros throw notices if the conversion fails...
------------------------------------------------------------------------
[2010-10-10 00:45:35] rowan dot collins at gmail dot com
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 this bug report at https://bugs.php.net/bug.php?id=53033&edit=1
Thread (7 messages)