From:
Operating system: Windows 7, Ubuntu 10
PHP version: 5.3.8
Package: Class/Object related
Bug Type: Bug
Bug description:Assignments of $this in some implicit __toString contexts get
broken
Description:
------------
Tested with php 5.3.8 on Windows 7, 5.4.0RC2 Win32, and 5.4.0RC2 (cli) on
Ubuntu 10.04.3
When an object is implicitly used as a string when passed to some php
functions, __toString is called as expected, but any assignments of $this
from within __toString are eventually broken and point instead to the
string that gets returned. This behavior occurs only in some php functions,
and never if you cast as string while passing into the functions.
This behavior has been observed when passing objects into at least these
php functions:
str_replace, preg_replace, preg_match_all, sprintf.
There are many php string functions that do not behave this way (they allow
__toString-created references to $this persist as references to the
object).
For extra fun, play around with htmlspecialchars, echo-ing the output... I
suspect that's a whole different issue, but related (possible memory leak
there).
Test script:
---------------
<?php
class Primary {
public function __toString() {
$GLOBALS['anObject'] = $this;
return 'toString output from Primary';
}
}
$Foo = new Primary;
str_replace('', '', $Foo);
var_dump($GLOBALS['anObject']);
// expected to be object of class Primary, actually string
Expected result:
----------------
object(Primary)#1 (0) { }
Actual result:
--------------
string(28) "toString output from Primary"
--
Edit bug report at https://bugs.php.net/bug.php?id=60475&edit=1
--
Try a snapshot (PHP 5.4):
https://bugs.php.net/fix.php?id=60475&r=trysnapshot54
Try a snapshot (PHP 5.3):
https://bugs.php.net/fix.php?id=60475&r=trysnapshot53
Try a snapshot (trunk):
https://bugs.php.net/fix.php?id=60475&r=trysnapshottrunk
Fixed in SVN:
https://bugs.php.net/fix.php?id=60475&r=fixed
Fixed in SVN and need be documented:
https://bugs.php.net/fix.php?id=60475&r=needdocs
Fixed in release:
https://bugs.php.net/fix.php?id=60475&r=alreadyfixed
Need backtrace:
https://bugs.php.net/fix.php?id=60475&r=needtrace
Need Reproduce Script:
https://bugs.php.net/fix.php?id=60475&r=needscript
Try newer version:
https://bugs.php.net/fix.php?id=60475&r=oldversion
Not developer issue:
https://bugs.php.net/fix.php?id=60475&r=support
Expected behavior:
https://bugs.php.net/fix.php?id=60475&r=notwrong
Not enough info:
https://bugs.php.net/fix.php?id=60475&r=notenoughinfo
Submitted twice:
https://bugs.php.net/fix.php?id=60475&r=submittedtwice
register_globals:
https://bugs.php.net/fix.php?id=60475&r=globals
PHP 4 support discontinued:
https://bugs.php.net/fix.php?id=60475&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=60475&r=dst
IIS Stability:
https://bugs.php.net/fix.php?id=60475&r=isapi
Install GNU Sed:
https://bugs.php.net/fix.php?id=60475&r=gnused
Floating point limitations:
https://bugs.php.net/fix.php?id=60475&r=float
No Zend Extensions:
https://bugs.php.net/fix.php?id=60475&r=nozend
MySQL Configuration Error:
https://bugs.php.net/fix.php?id=60475&r=mysqlcfg