php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18866 Assignment problem
Submitted: 2002-08-12 05:27 UTC Modified: 2002-08-12 09:29 UTC
From: chris dot lucky at aon dot at Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.2.1 OS: Win XP prof.
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: chris dot lucky at aon dot at
New email:
PHP Version: OS:

 

 [2002-08-12 05:27 UTC] chris dot lucky at aon dot at
I have 2 classes linked together. In a search-method an object (or a list of them) is created and returned.
The structure was build correct in the search-method, but when I assign another value to the mom-object it seams that there are different child-objects.
The following sample shows it:

####################################
#
class Mom
{
	// Properties:

	var $title;
	var $myChild;


	// Methods:

function Mom ($title = "")
{
	$this->title = $title;
}

function init (&$child)
{
	$this->myChild = &$child;
}

function search ()
{
	$mom = new Mom ("test10");
	$child = new Child ();
	
	$mom->init (&$child);
	$child->init (&$mom);

	return $mom;
}

}	# end class Mom


####################################
#
class Child
{
	// Properties:

	var $myMom;


	// Methods:

function Child ()
{
}

function init (&$mom)
{
	$this->myMom = &$mom;
}

}	# end class Child


####################################
#
#   test code:

    $m = new Mom ();
    $mom = $m->search ();
    $t0 = $mom->title;
    $t1 = $mom->myChild->myMom->title;

    echo "$t0 = $t1<br>\n"; # correct values


    $mom->title = "test1";
    $t0 = $mom->title;
    $t1 = $mom->myChild->myMom->title;

    echo "$t0 != $t1<br>\n"; # why are t0 and t1 different?


Is there a bug in my code or in php?
Thanks,
Chris

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-08-12 09:29 UTC] rodif_bl@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You need to add a few more refrences.
$m = $m->search(); // this does a copy too
$m = &$m->search(); // this will make it work

you will  need to change
function search()
to
function &search()

It works for me after these changes
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 27 16:01:27 2024 UTC