|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 09 18:00:02 2025 UTC |
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; }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(); ?>