Bug #53033 [Ver]: Mathematical operations convert objects to integers

From: Date: Sun, 26 Feb 2017 11:25:49 +0000
Subject: Bug #53033 [Ver]: Mathematical operations convert objects to integers
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207561@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=53033&edit=1 ID: 53033 Updated by: nikic@php.net 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: If you want to work on this, here's some pointers on what needs to be done. The general issue is that cast_object handlers are currently limited to casting to integer or float, but not "cast to integer/float, whichever is more appropriate". To fix this, a new casting mode needs to be introduced. The procedure would be something like this: * Add an _IS_NUMBER pseudo-type to zend_types.h * In zend_operators.c, in convert_scalar_to_number() change the IS_OBJECT case to perform a cast to _IS_NUMBER (the general code for this is similar as in convert_to_long_base). Similar adjustments are necessary in zendi_convert_scalar_to_number(). * In sxe.c change the cast_object() function to handle the _IS_NUMBER cast (by calling convert_scalar_to_number()). * Check the codebase for other cast_object handlers and see if they need to be adjusted to support casting to _IS_NUMBER. Previous Comments: ------------------------------------------------------------------------ [2017-02-26 05:00:47] qheaden at phaseshiftsoftware dot com 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. ------------------------------------------------------------------------ [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

« previous php.bugs (#207561) next »