|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2013-10-14 13:23 UTC] phpmpan at mpan dot pl
[2013-10-14 13:25 UTC] worldoffame at hotmail dot com
[2013-10-14 13:33 UTC] johannes@php.net
-Status: Open
+Status: Wont fix
[2013-10-14 13:33 UTC] johannes@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 14:00:01 2025 UTC |
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)