Bug #60475 [Opn]: Assignments of $this in some implicit __toString contexts get broken
Edit report at https://bugs.php.net/bug.php?id=60475&edit=1
ID: 60475
Updated by: cmb@php.net
Reported by: ctrahey at rubicon dot com
Summary: Assignments of $this in some implicit __toString
contexts get broken
Status: Open
Type: Bug
Package: Class/Object related
Operating System: Windows 7, Ubuntu 10
PHP Version: 5.3.8, 5.4.0RC2, 5.5.0-dev
Block user comment: N
Private report: N
New Comment:
The issue appears to be resolved in PHP 7: <http://3v4l.org/D7nhN>.
Previous Comments:
------------------------------------------------------------------------
[2014-01-22 11:30:00] joe dot bowman at edigitalresearch dot com
This issue can be replicated in PHP version 5.4.19 and 5.5.3 (via php-fpm) on Centos 6.4,
specifically passing Objects into in_array (object has a __toString() method, and is being compared
to an array of strings).
------------------------------------------------------------------------
[2011-12-12 18:25:38] ctrahey at rubicon dot com
Tested against trunk (5.5.0-dev, revision 320923) compiled on Ubuntu 10.04.3 with same (incorrect)
result.
------------------------------------------------------------------------
[2011-12-08 16:58:08] ctrahey at rubicon dot com
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 this bug report at https://bugs.php.net/bug.php?id=60475&edit=1
Thread (4 messages)