php.net |  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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
48 - 18 = ?
Subscribe to this entry?

 
 [2019-03-27 14:19 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-06-14 11:57 UTC] sjon@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: sjon
 [2019-06-14 11:57 UTC] sjon@php.net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 09:01:26 2024 UTC