Doc #77808 [Opn->Csd]: Undocumented behaviour on assign by reference with magic getters
| From: | sjon@php.net | Date: | Fri, 14 Jun 2019 11:57:17 +0000 |
| Subject: | Doc #77808 [Opn->Csd]: Undocumented behaviour on assign by reference with magic getters | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-16745@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77808&edit=1
ID: 77808
Updated by: sjon@php.net
Reported by: christian dot mohr at insitu dot de
Summary: Undocumented behaviour on assign by reference with
magic getters
-Status: Open
+Status: Closed
Type: Documentation Problem
Package: Documentation problem
PHP Version: 7.1.27
-Assigned To:
+Assigned To: sjon
Block user comment: N
Private report: N
New Comment:
I'm pretty sure this has been addressed in 7.3+; see for example https://3v4l.org/rMHdN it throws:
Fatal error: Uncaught Error: Cannot assign by reference to overloaded object in /in/rMHdN:14
Previous Comments:
------------------------------------------------------------------------
[2019-03-27 14:19:52] christian dot mohr at insitu dot de
Description:
------------
Assigning by reference to an non-existing property of an object works as expected (like any
assign-by-value). But if the object contains a magic __get() method the behaviour is undocumented.
Why is the magic getter invoked? Why does the assignment fail? Is there any way to get the expected
behaviour with the magic getter in place?
Removing either the magic getter OR the assign-by-reference "&" (assign-by-value
instead) produces the expected result.
Test script:
---------------
Class CLS
{
public function &__get($key) {
echo "__get " . $key . PHP_EOL;
return $this->$key;
}
}
$magicTest = new CLS();
$test = "test";
$magicTest->v = & $test;
echo ($magicTest->v ?? "null") . PHP_EOL;
Expected result:
----------------
test
Actual result:
--------------
__get v
null
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77808&edit=1