Req #48442 [Com]: strpos does not attempt to cast needle as string if needle is an object
| From: | chx@php.net | Date: | Sun, 25 May 2014 01:41:26 +0000 |
| Subject: | Req #48442 [Com]: strpos does not attempt to cast needle as string if needle is an object | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-185921@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=48442&edit=1
ID: 48442
Comment by: chx@php.net
Reported by: deadowlsurvivor at gmail dot com
Summary: strpos does not attempt to cast needle as string if
needle is an object
Status: Open
Type: Feature/Change Request
Package: *General Issues
Operating System: Irrelevant
PHP Version: Any
Block user comment: N
Private report: N
New Comment:
1. AFAIK when casting something that is neither a string nor a number into an integer it can only be
0 or 1 and are you really looking for chr(0) when calling strpos(foo, FALSE)? I'd say a warning
would be much better than casting. Anyways...
2. Objects have no ways to be cast into an integer however they may have a way to be cast into a
string so instead of trying to cast integer which can only fail why not try to cast into string?
This would be a very small BC break because previously this emitted an error message so probably it
is not in any code.
Previous Comments:
------------------------------------------------------------------------
[2014-04-30 23:33:26] deadowlsurvivor at gmail dot com
Wow, it's been a while since I posted this. It's more consistent with other type casting
operations to type cast to the traditional expected value if applicable (even integers as an ordinal
value). However, to obtain this consistency it would hurt backwards compatibility. I would be
gracious if there was a release of PHP that focused on consistency issues that have been neglected
for reasons of backwards compatibility, which would likely entail a new major version. If
that's not the direction that the PHP developers are interested in, you can go ahead and close
this or otherwise assign it to a new larger version number.
------------------------------------------------------------------------
[2014-04-30 22:45:58] levim@php.net
Is this something you are still interested in seeing?
Personally this looks nonsensical; objects aren't integers and so you need to do any conversion
yourself.
------------------------------------------------------------------------
[2009-06-01 00:50:16] deadowlsurvivor at gmail dot com
Description:
------------
Currently, if you attempt to set
Reproduce code:
---------------
class foo {
private $bar;
public function __construct($bar) {
$this->bar = $bar;
}
public function __toString() {
return $bar;
}
}
$a = new foo('abc');
$b = new foo('ab');
$pos = strpos($a,$b);
Expected result:
----------------
$pos holds the value of 0.
Actual result:
--------------
While $a is cast to a string, PHP attempts to cast $b into an integer, as documented. However,
wouldn't it make sense to attempt to cast $b into a string if casting it as an integer fails?
Or even to attempt to cast it into a string first?
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=48442&edit=1