PHP :: Bug #34358 :: Fatal error: Cannot re-assign $this
| Bug #34358 | Fatal error: Cannot re-assign $this | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Submitted: | 2005-09-03 13:36 UTC | Modified: | 2010-09-03 23:34 UTC |
|
||||||||||
| From: | pacha dot shevaev at gmail dot com | Assigned: | dmitry (profile) | |||||||||||
| Status: | Closed | Package: | Scripting Engine problem | |||||||||||
| PHP Version: | 5.* | OS: | * | |||||||||||
| Private report: | No | CVE-ID: | None | |||||||||||
[2005-09-03 13:36 UTC] pacha dot shevaev at gmail dot com
Description:
------------
PHP 5.1RC1 throws fatal error when some variable gets assigned with $this by reference. However there's a workaround for this fatal error which is very simple: using a helper function which simply returns the passed argument by reference. I'm a bit lost here - what is the expected behavior?
Yes, it may seem a bit weird to assign objects by reference in PHP5 but if you keep your code base PHP4 compatible you know what i mean.
Reproduce code:
---------------
<?php
function & getRef(&$ref) {
return $ref;
}
class Foo {
function Foo() {
//$ref =& getRef($this); //works just fine
$ref =& $this; //throws "cannot re-assign $this" fatal
$ref->test();
}
function test() {
echo 'test';
}
}
$foo = new Foo();
?>
Expected result:
----------------
test
Actual result:
--------------
Fatal error: Cannot re-assign $this
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2005-09-15 13:37 UTC] pacha dot shevaev at gmail dot com
I still find it a bug. I need a reference to $this for BC with PHP4 in the following piece of code: function &getRootDataSource() { $root =& $this; while ($root->parent != NULL) { $root =& $root->parent; } return $root; }[2005-09-15 14:27 UTC] sniper@php.net
[2005-09-15 15:23 UTC] pacha dot shevaev at gmail dot com
[2005-09-15 15:28 UTC] derick@php.net
[2005-09-15 15:32 UTC] pacha dot shevaev at gmail dot com
And why does the following code work then??? <?php class Foo { function Foo() { $this->ref =& $this; $this->ref->test(); } function test() { echo 'test'; } } $foo = new Foo(); ?>[2005-10-03 09:40 UTC] rasmus@php.net
[2005-10-03 12:14 UTC] rasmus@php.net
[2005-10-03 12:42 UTC] derick@php.net
[2005-10-04 08:01 UTC] pacha dot shevaev at gmail dot com
[2005-10-21 10:07 UTC] dmitry@php.net
[2010-09-03 23:12 UTC] az_startreker at yahoo dot com
[2010-09-03 23:34 UTC] pajoye@php.net