|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #77808 Undocumented behaviour on assign by reference with magic getters
Submitted: 2019-03-27 14:19 UTC Modified: 2019-06-14 11:57 UTC
From: christian dot mohr at insitu dot de Assigned: sjon (profile)
Status: Closed Package: Documentation problem
PHP Version: 7.1.27 OS:
Private report: No CVE-ID: None
 [2019-03-27 14:19 UTC] christian dot mohr at insitu dot de
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:

Actual result:
__get v


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-06-14 11:57 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: sjon
 [2019-06-14 11:57 UTC]
I'm pretty sure this has been addressed in 7.3+; see for example it throws:

Fatal error: Uncaught Error: Cannot assign by reference to overloaded object in /in/rMHdN:14
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Sep 20 21:01:27 2019 UTC