php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50606 strange behavior of reference and ? operator
Submitted: 2009-12-29 13:02 UTC Modified: 2009-12-29 14:52 UTC
From: grzegorz at heex dot pl Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.1 OS: Win XP
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: grzegorz at heex dot pl
New email:
PHP Version: OS:

 

 [2009-12-29 13:02 UTC] grzegorz at heex dot pl
Description:
------------
That code works:

class A{
	private $data;

	public function __construct() {
		$this->data = new stdClass();
	}

	public function & __get($property) {
		if (isset($this->data->$property)) {
			return $this->data->$property;
		}
		return null;
	}

	public function __set($property,$value) {
		$this->data->$property = $value;
	}
}

function chgTxt(& $txt) {
	$txt = '-----';
}

$o = new A();

$o->int = 1;
$o->int++;
echo $o->int,'<br />';//result: 2 - reference in A::get() works fine

$o->txt = 'string';
chgTxt($o->txt);
echo $o->txt,'<br />';//result: '----' - reference works

Reproduce code:
---------------
That code doesn't work:

class A{
	private $data;

	public function __construct() {
		$this->data = new stdClass();
	}

	public function & __get($property) {
		return isset($this->data->$property) ? $this->data->$property : null;
	}

	public function __set($property,$value) {
		$this->data->$property = $value;
	}
}

function chgTxt(& $txt) {
	$txt = '-----';
}

$o = new A();

$o->int = 1;
$o->int++;
echo $o->int,'<br />';//result: 2 - reference in A::get() works fine

$o->txt = 'string';
chgTxt($o->txt);
echo $o->txt,'<br />';//result: 'string' - reference doesn't work

Expected result:
----------------
2
----

Actual result:
--------------
2
string

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-29 13:41 UTC] jani@php.net
You should try with error_reporting = E_ALL once:

Notice: Only variable references should be returned by reference...
Notice: Indirect modification of overloaded property..

 [2009-12-29 14:52 UTC] grzegorz at heex dot pl
The first code (IF...ELSE) works and there is no Notice.
Is there a bug in ? operator.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Dec 12 14:00:02 2025 UTC