php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71477 Inconsist behavior during assign-by-reference in __get()
Submitted: 2016-01-28 10:10 UTC Modified: 2016-03-22 09:10 UTC
From: arepo at nologin dot ru Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 7.0.2 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: arepo at nologin dot ru
New email:
PHP Version: OS:

 

 [2016-01-28 10:10 UTC] arepo at nologin dot ru
Description:
------------
In PHP 7+ assign-by-ref creates a property BEFORE calling $this->getProp($name). This behavior is described in the documentations only for arrays.

You can see result in https://3v4l.org/PBZRS 

Test script:
---------------
<?php
class MagicByRef {
	private $prop = 'hello';

	private function &getProp($name)
	{
		printf(
			"%s: %s is %s\n",
			__METHOD__,
			$name,
			property_exists($this, $name) ? 'exists' : 'not exists'
		);

		return $this->prop;
	}

	public function __set($name, $value)
	{
		echo __METHOD__ , "($name, $value)\n";
	}

	public function &__get($name)
	{
		echo __METHOD__ , "($name)\n";

		$this->$name = &$this->getProp($name);

		return $this->$name;
	}
}

echo 'Assignment by reference: $this->$name = &$this->getProp($name)', ":\n";
$m = new MagicByRef;

$m->hello;

Expected result:
----------------
Assignment by reference: $this->$name = &$this->getProp($name):
MagicByRef::__get(hello)
MagicByRef::getProp: hello is not exists

Actual result:
--------------
Assignment by reference: $this->$name = &$this->getProp($name):
MagicByRef::__get(hello)
MagicByRef::getProp: hello is exists

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-28 10:22 UTC] requinix@php.net
-Package: PHP Language Specification +Package: Scripting Engine problem
 [2016-03-22 09:10 UTC] arepo at nologin dot ru
-Status: Open +Status: Closed
 [2016-03-22 09:10 UTC] arepo at nologin dot ru
Resolved in 7.0.4
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 15:00:01 2026 UTC