php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #12905 "return" returns a copy of an object instead of a reference
Submitted: 2001-08-22 12:13 UTC Modified: 2001-08-22 16:19 UTC
From: olli at ukgamer dot net Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 4.0.6 OS: Slackware 7
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: olli at ukgamer dot net
New email:
PHP Version: OS:

 

 [2001-08-22 12:13 UTC] olli at ukgamer dot net
<?
Class ChildClass {
	var $value;
	
	function ChildClass($value){
		$this->value = $value;
	}
}

Class ParentClass {
	var $children;
	
	function ParentClass(){
		$this->children = array();
	}
	
	function AddChild($value){
		$child = &new ChildClass($value);
		array_push($this->children, &$child);
		return $child;
	}
}


$p1 = &new ParentClass();

$c1 = &$p1->AddChild('set in constructor');
$c1->value = 'set elsewhere';

echo "the reference to the object created in AddBand and stored in the parentclass' array is different to that returned by the AddBand function<br>";
echo $p1->children[0]->value."<br>";
echo $c1->value;

?>

"return" is returning a copy of the object created in parentclass->AddChild instead of a reference. "return &$object" is flagged as invalid by the php interpreter.

thanks a lot
olli holliday

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-08-22 12:20 UTC] olli at ukgamer dot net
renaming the function:

function AddChild(...

to

function &AddChild(...
makes return return by reference instead of by value, i didnt think to do this so it appears its my fault:)

i've changed this bug to a documentation problem tho cos i cant find any reference to this in the docs.

 [2001-08-22 16:19 UTC] jeroen@php.net
Please read: http://www.php.net/manual/en/language.references.return.php

[By the way, a user complains that is hard to read. If you or anyone else has a better text, simply mail it to phpdoc@lists.php.net, and we'll put it online.]
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 15:01:30 2024 UTC