|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-06-01 00:50 UTC] 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?
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 20:00:01 2025 UTC |
I totally agree with the initial poster. objects implementing __toString() should be converted to a string automatically when used for parameters which are expected to be string. my example is: <?php class stringable { function __toString() { return "abc"; } } $s = new stringable(); // AS EXPECTED: converts the object into a string executes the replace in the same fashion with a plain php string var_dump(str_replace('a', 'x', $s)); // print xbc // both of the following lines emit a warning but are expected to use the given object as a string and proceed // both emit: Notice: Object of class stringable could not be converted to int var_dump(strpos('abcd', $s)); var_dump(stripos('abcd', $s)); see https://3v4l.org/Bf3Ah