php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38176 overloading is ignored when using reference
Submitted: 2006-07-21 15:04 UTC Modified: 2006-07-24 07:37 UTC
From: pedro dot leite dot rocha at gmail dot com Assigned: dmitry (profile)
Status: Not a bug Package: Class/Object related
PHP Version: 5.1.4 OS: Linux 2.6.15-26-686
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: pedro dot leite dot rocha at gmail dot com
New email:
PHP Version: OS:

 

 [2006-07-21 15:04 UTC] pedro dot leite dot rocha at gmail dot com
Description:
------------
When using overloading with the special function __set, and passing the value by reference, the overload is ignored, and a new attribute for the class object is created.

I'm not sure if this is a bug, but I couldn't find anything like it in the documentation, or in the bug list. Appreciate the help.
[]'s

Reproduce code:
---------------
class A {
	private $v;
	function __construct() {
		echo "constructing...\n";
	}
	function __set($key, $value) {
		echo "setting ".$key."\n";
		$this->v[$key] = $value;
	}
}

$foo = "bar";
$a = new A;
echo "without reference:\n";
$a->user = $foo;
echo "with reference:\n";
$a->user =& $foo;
var_dump($a);


Expected result:
----------------
constructing...
without reference:
setting user
with reference:
object(A)#1 (2) {
  ["v:private"]=>
  array(1) {
    ["user"]=>
    &string(3) "bar"
  }
}


Actual result:
--------------
constructing...
without reference:
setting user
with reference:
object(A)#1 (2) {
  ["v:private"]=>
  array(1) {
    ["user"]=>
    string(3) "bar"
  }
  ["user"]=>
  &string(3) "bar"
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-07-24 07:37 UTC] dmitry@php.net
This is not a bug (may be a logical inconsistency).

Method __set() is used for modification values of overloaded properties and it cannot be used to assigment by reference.

Implement method __get and you will get "Fatal error: Cannot assign by reference to overloaded object ...".
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Mar 14 09:01:29 2025 UTC