php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #53770 __set isn't called on ref assignment
Submitted: 2011-01-17 22:50 UTC Modified: 2021-07-28 15:48 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (33.3%)
From: stormingphoenix at yahoo dot com Assigned:
Status: Verified Package: Class/Object related
PHP Version: 5.3.5 OS: All
Private report: No CVE-ID: None
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:
43 + 45 = ?
Subscribe to this entry?

 
 [2011-01-17 22:50 UTC] stormingphoenix at yahoo dot com
Description:
------------
When using __set($strProp, &$strValue), you are given the error: 

Method foo::__set() cannot take arguments by reference

If, you use $obj->property =& $that it will not call __set at all, but rather auto-create 'property';



Not a major issue, but at the least the __set not being called may need to be fixed.

Test script:
---------------
	class foo {
		private $var = NULL;
		function __set($strProp, $strValue) {
			var_dump('here');
			unset($strValue);
		}
	}
	
	class bar {
		var $that = 'that';
	}
	
	error_reporting(E_STRICT);
	
	$that = 'that';
	
	$foo = new foo();
	
	$foo->Property =& $that;
	var_dump($foo->Property);
	
        var_dump($that);
	
	$bar = new bar();
	$foo->Property =& $bar;
	
	var_dump($bar);

Expected result:
----------------
string(4) "here" 
undefined variable: $bar

Actual result:
--------------
string(4) "that"
object(bar)#7 (1) { ["that"]=> string(4) "that" } 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-28 15:48 UTC] cmb@php.net
-Summary: __set Doesn't Allow Pass By Reference (Even On Objects) And Isn't Called On Ref +Summary: __set isn't called on ref assignment -Status: Open +Status: Verified -Type: Feature/Change Request +Type: Documentation Problem
 [2021-07-28 15:48 UTC] cmb@php.net
> Not a major issue, but at the least the __set not being called
> may need to be fixed.

Even if we wanted, we couldn't fix this for BC reasons.  And I
wouldn't even want to have that fixed.  We should document the
behavior, though.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 09 13:01:28 2024 UTC