php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70321 Magic getter breaks reference to array property
Submitted: 2015-08-21 16:42 UTC Modified: 2015-08-21 19:06 UTC
From: hrach dot cz at gmail dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 7.0.0RC1 OS: win10
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:
45 - 37 = ?
Subscribe to this entry?

 
 [2015-08-21 16:42 UTC] hrach dot cz at gmail dot com
Description:
------------
See the testcase. Worked ok in 7.0.0-beta3

Test script:
---------------
<?php
class foo
{
	private $bar;
	public function __construct()
	{
		$this->bar = new bar();
	}
	public function & __get($key)
	{
		$bar = $this->bar;
		return $bar;
	}
}
class bar { public $onBaz = []; }

$foo = new foo();
$foo->bar->onBaz[] = function() {};
var_dump($foo->bar->onBaz);

Expected result:
----------------
array(1) {
  [0]=>
  object(Closure)#4 (0) {
  }
}

Actual result:
--------------
array(0) {
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-21 17:53 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2015-08-21 17:53 UTC] nikic@php.net
The behavior is the same for all PHP 7 versions: https://3v4l.org/GBpN8

Did you mean to write $bar =& $this->bar? If you don't use a by-reference assignment, you won't be able to indirectly modify the property.
 [2015-08-21 18:24 UTC] hrach dot cz at gmail dot com
No, on Windows the bahavior has changed:

/d/storage $ php --version
PHP 7.0.0beta3 (cli) (built: Aug  5 2015 12:17:06)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies
/d/storage $ php test.php
array(1) {
  [0]=>
  object(Closure)#3 (0) {
  }
}
/d/storage $ php --version
PHP 7.0.0RC1 (cli) (built: Aug 20 2015 12:42:50)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies
/d/storage $ php test.php
array(0) {
}

On Linux probably too, since travis was ok!
- ok: https://travis-ci.org/nextras/orm/jobs/72314843
- broken: https://travis-ci.org/nextras/orm/jobs/76666269

I do not meant that proposed change. Since I'm returning object by magic getter, it's has no meaning to put the reference there. The reference would be important for that magic property, not the holding object itself, wouldn't be?
 [2015-08-21 18:42 UTC] bugs dot php dot net at majkl578 dot cz
I did some git-bisecting (because we originally thought it was caused by a fix for #70262) and it lead me to 38151a38ec31af33e10513cc39e7cac8f819f120, a commit from 4 Mar 2014. Anything newer appears to return an empty array...

Tried with ./configure --disable-all --enable-cli on Debian x86_64.
 [2015-08-21 19:00 UTC] j dot tvr at centrum dot cz
It turns out what matter is NTS vs. TS build. If we modify the testcase to print more info

echo "PHP_VERSION: " . PHP_VERSION . "\n";
echo "PHP_ZTS: " . PHP_ZTS . "\n";
echo "\n";

we get the following results:


$ php test.php
PHP_VERSION: 7.0.0RC1
PHP_ZTS: 0

array(0) {
}

$ php test.php
PHP_VERSION: 7.0.0RC1
PHP_ZTS: 1

array(0) {
}

$ php test.php
PHP_VERSION: 7.0.0beta3
PHP_ZTS: 0

array(0) {
}

$ php test.php
PHP_VERSION: 7.0.0beta3
PHP_ZTS: 1

array(1) {
  [0]=>
  object(Closure)#3 (0) {
  }
}
 [2015-08-21 19:06 UTC] nikic@php.net
-Status: Feedback +Status: Open
 [2015-08-21 19:06 UTC] nikic@php.net
> I do not meant that proposed change. Since I'm returning object by magic getter, it's has no meaning to put the reference there. The reference would be important for that magic property, not the holding object itself, wouldn't be?

You're right, I misread the example.
 [2015-08-22 22:55 UTC] bwoebi@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=66754585f889e56ddaf3442fd4ac7b9adf93417b
Log: Fixed bug #70321 (Magic getter breaks reference to array property) Note that the UNEXPECTED(zv) was turned into EXPECTED(zv), as zv is mostly set in the context where it is used and usually anyway is checked first with OP*_TYPE == IS_VAR; or maybe just completely remove that (UN)EXPECTED at this place...
 [2015-08-22 22:55 UTC] bwoebi@php.net
-Status: Open +Status: Closed
 [2015-08-23 03:43 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=e26a04f9ecb18fe6d233fb5840f71ad216f27938
Log: Revert &quot;Fixed bug #70321 (Magic getter breaks reference to array property)&quot;
 [2015-08-23 03:49 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=55f88141c5a308ae507ef23606b190387e6c37dc
Log: Re-Fixed bug #70321 (Magic getter breaks reference to array property)
 [2015-09-03 18:10 UTC] ab@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=55f88141c5a308ae507ef23606b190387e6c37dc
Log: Re-Fixed bug #70321 (Magic getter breaks reference to array property)
 [2015-09-03 18:10 UTC] ab@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=e26a04f9ecb18fe6d233fb5840f71ad216f27938
Log: Revert &quot;Fixed bug #70321 (Magic getter breaks reference to array property)&quot;
 [2015-09-03 18:10 UTC] ab@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=66754585f889e56ddaf3442fd4ac7b9adf93417b
Log: Fixed bug #70321 (Magic getter breaks reference to array property) Note that the UNEXPECTED(zv) was turned into EXPECTED(zv), as zv is mostly set in the context where it is used and usually anyway is checked first with OP*_TYPE == IS_VAR; or maybe just completely remove that (UN)EXPECTED at this place...
 [2016-07-20 11:36 UTC] davey@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=55f88141c5a308ae507ef23606b190387e6c37dc
Log: Re-Fixed bug #70321 (Magic getter breaks reference to array property)
 [2016-07-20 11:36 UTC] davey@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=e26a04f9ecb18fe6d233fb5840f71ad216f27938
Log: Revert &quot;Fixed bug #70321 (Magic getter breaks reference to array property)&quot;
 [2016-07-20 11:36 UTC] davey@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=66754585f889e56ddaf3442fd4ac7b9adf93417b
Log: Fixed bug #70321 (Magic getter breaks reference to array property) Note that the UNEXPECTED(zv) was turned into EXPECTED(zv), as zv is mostly set in the context where it is used and usually anyway is checked first with OP*_TYPE == IS_VAR; or maybe just completely remove that (UN)EXPECTED at this place...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 20:01:29 2024 UTC