php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #46524 is_numeric doesn't take objects as parameter
Submitted: 2008-11-08 20:04 UTC Modified: 2016-12-31 00:36 UTC
Votes:3
Avg. Score:3.0 ± 1.4
Reproduced:2 of 3 (66.7%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: kalle@php.net Assigned:
Status: Open Package: Variables related
PHP Version: 5.3.0alpha2 OS: Irrelevant
Private report: No CVE-ID: None
 [2008-11-08 20:04 UTC] kalle@php.net
Description:
------------
I'm not sure whenever this is intended, but is_numeric() doesn't check if a value is numeric if the value is an object and has a magic __toString() method.

I've created a patch and a test to illustrate this:
Index: type.c
===================================================================
RCS file: /repository/php-src/ext/standard/type.c,v
retrieving revision 1.30.2.2.2.3.2.9
diff -u -r1.30.2.2.2.3.2.9 type.c
--- type.c	21 Oct 2008 22:08:38 -0000	1.30.2.2.2.3.2.9
+++ type.c	8 Nov 2008 16:33:37 -0000
@@ -312,6 +312,11 @@
 			RETURN_TRUE;
 			break;
 
+		case IS_OBJECT:
+			convert_to_string_ex(arg);
+
+			/* Break intentionally missing */
+
 		case IS_STRING:
 			if (is_numeric_string(Z_STRVAL_PP(arg), Z_STRLEN_PP(arg), NULL, NULL, 0)) {
 				RETURN_TRUE;

And the test:
/ext/standard/tests/misc/is_numeric_object.phpt

--TEST--
is_numeric() should accept objects
--FILE--
<?php
class Test
{
	public function __toString()
	{
		return('1337');
	}
}

$test = new Test;
var_dump(is_numeric($test));
?>
--EXPECT--
bool(true)



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-12-31 00:36 UTC] cmb@php.net
-Package: Feature/Change Request +Package: Variables related
 [2018-09-05 23:28 UTC] carusogabriel@php.net
IMO we can also expand other functions, like `is_string` to also support objects, witch current it doesn't: https://3v4l.org/R1kJr

I'm gonna be expanding the proposed patch and open for discussion in GitHub/internals@ mailing list.
 [2019-06-27 17:02 UTC] dwilks at intacct dot com
Please look at https://bugs.php.net/bug.php?id=78219 also.  It's of the same type, trying to pass an extension-based class instance into a bool parameter.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Oct 04 08:01:28 2024 UTC