|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2000-10-31 18:00 UTC] twoerner at redhat dot de
<?
class C_CART_ITEM {
var $pid;
var $id=0;
Function C_CART_ITEM() { }
}
class C_SESSION {
var $cart1 = array();
var $cart2 = array();
Function C_SESSION() { }
Function update() {
$item = new C_CART_ITEM();
$item->pid = "".time();
array_push($this->cart1, $item);
array_push($this->cart2, $item);
}
Function show() {
echo("ITEMS:<BR><TABLE CELLSPACING=2 CELLPADDING=0>");
$i = 0;
while ($i < MAX(count($this->cart1),count($this->cart2))) {
$item = &$this->cart1[$i];
$item->id += 1;
echo("<TR><TD>".$item->pid." : ".$item->id."</TD>");
$item = &$this->cart2;
$item[$i]->id += 1;
echo("<TD>".$item[$i]->pid." : ".$item[$i]->id."</TD</TR>");
$i++;
}
echo("</TABLE><P>");
}
}
session_start();
if (!$session) {
$session = new C_SESSION();
session_register("session");
} else
$session->update();
echo "SESSION-ID: ".session_id()."<BR>";
$session->show();
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 10:00:02 2025 UTC |
Stage 1: ======= Ok, this version works with 4.0.4dev-200012020345. But it was an excerpt from my code. Stage 2: ======= Now i have extended the example and got the same error again :-( Please have a look at the code below: <? class C_CART_ITEM { var $pid; var $id=0; Function C_CART_ITEM() { } } class C_SESSION { var $cart1 = array(); var $cart2 = array(); Function C_SESSION() { } Function update() { $item = new C_CART_ITEM(); $item->pid = "".time(); array_push($this->cart1, $item); array_push($this->cart2, $item); } Function test() { for($i=0; $i<count($this->cart1); $i++) { $item = &$this->cart1[$i]; $item->pid .= "a"; } $item = $this->cart2; for($i=0; $i<count($item); $i++) { $item[$i]->pid .= "a"; } } Function show() { echo("ITEMS:<BR><TABLE CELLSPACING=2 CELLPADDING=0>"); $i = 0; while ($i < MAX(count($this->cart1),count($this->cart2))) { $item = &$this->cart1[$i]; $item->id += 1; echo("<TR><TD>".$item->pid." :".$item->id."</TD>"); $item = &$this->cart2; $item[$i]->id += 1; echo("<TD>".$item[$i]->pid." :".$item[$i]->id."</TD</TR>"); $i++; } echo("</TABLE><P>"); } } session_start(); if (!$session) { $session = new C_SESSION(); session_register("session"); } else { $session->test(); $session->update(); } echo "SESSION-ID: ".session_id()."<BR>"; $session->show(); ?> Have a look at the new test function. The line '$item = $this->cart2;' was first a mistake (it works on a copy and therefore doe not append the 'a' to the pid of other version). If this line is in there the same old problem is back again. The output looks like this: SESSION-ID: f0cf1206f566d170bf6fde86a5f76035 ITEMS: : 975780490 :7 : 975780490 :6 : 975780491 :5 : 975780491 :4 : 975780491 :3 : 975780491 :2 975780492 :1 975780492 :1 BUT: If you rename $item with $cart all is ok. If you replace '$item = $this->cart2;' with '$item = &$this->cart2;' all is ok. Stage 3: ======= I tried a third version with a inc and dec function in the C_CART_ITEM class wich increased and decreases $id and i called the dec and the inc function in C_SESSION->test and i got this error: Fatal error: Call to a member function on a non-object in ... Ouch!this is rather a confusion how references work than a bug if you had var_dump()ed your $session you would have seen that session handling cannot be involved here what you are doing is a) creating a reference $item to an $item in cart1 b) and then storing the whole cart2 therein chaning (b) to $item = &$this->cart2; works for me Function test() { for($i=0; $i<count($this->cart1); $i++) { $item = &$this->cart1[$i]; // (a) $item->pid .= "a"; } $item = $this->cart2; // (b) for($i=0; $i<count($item); $i++) { $item[$i]->pid .= "a"; } }