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:2
Avg. Score:2.5 ± 1.5
Reproduced:1 of 2 (50.0%)
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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
21 - 1 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Jun 26 15:01:39 2019 UTC