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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 17:01:29 2024 UTC