php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #10088 Object linking
Submitted: 2001-03-31 04:58 UTC Modified: 2001-11-17 12:46 UTC
From: admin at room41 dot net Assigned:
Status: Closed Package: Class/Object related
PHP Version: 4.0.4pl1 OS: W2K
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: admin at room41 dot net
New email:
PHP Version: OS:

 

 [2001-03-31 04:58 UTC] admin at room41 dot net
The script shown attempts to implement a linked list, but the problem is more general and occurs in many similar situations.

<?
$d1 = @new obj("one");	//	Suppress the warning over the missing argument
$d2 = new obj("two",   $d1);
$d3 = new obj("three", $d2);
$d4 = new obj("four",  $d3);
$d5 = new obj("five",  $d4);

// Dump what should be the entire linked list
$d1->dump();
print("<p>");

// Now to demonstrate the problem:
// No matter where you begin the dump() from, only the
// $this node and its immediate successor are printed.
$d2->dump(); print("<p>");
$d3->dump(); print("<p>");

class obj {
  var $next;
  var $name;
	
  function &obj($name,&$prev) {
    //	Add each new object as the "next" pointer of the preceeding one

    this->name = $name;
    if ($prev) $prev->next = $this;
  }
function dump() {
  for ( $ptr = $this; $ptr; $ptr = $ptr->next) print($ptr->name . "<br>");
  }
}
?>

It seems that when an object's data is manipulated by a different object - even as here, of the same class - the changes made to the object are lost when its context expires.

Configuration: Absolutely standard
Webserver: Apache and IIS ... same problem in both, and under cgi and as a module
Aggravation factor: Immense

Cheer chaps for an otherwise excellent language!

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-11-17 12:46 UTC] mfischer@php.net
Your example is wrong. You're copying object everywhere like it is christmas day.

Here's the corrected example for your convenience:
<?
$d1 = &new obj("one");	//	Suppress the warning over the missing argument
$d2 = &new obj("two",   $d1);
$d3 = &new obj("three", $d2);
$d4 = &new obj("four",  $d3);
$d5 = &new obj("five",  $d4);

// Dump what should be the entire linked list
$d1->dump();
print("\n\n");

// Now to demonstrate the problem:
// No matter where you begin the dump() from, only the
// $this node and its immediate successor are printed.
$d2->dump(); print("\n\n");
$d3->dump(); print("\n\n");

class obj {
  var $next;
  var $name;
	
  function &obj($name,&$prev) {
    //	Add each new object as the "next" pointer of the preceeding one

    $this->name = $name;
    if ($prev) $prev->next = &$this;
  }
function dump() {
  for ( $ptr = &$this; $ptr; $ptr = &$ptr->next) print($ptr->name .
"\n");
  }
}
?>

Even if this example uses '&' where not needed it's good that way because not even a single object gets copied by accident.

Closed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 02:01:28 2024 UTC