ID: 7557
Updated by: andre
Reported By: [EMAIL PROTECTED]
Old-Status: Open
Status: Closed
Bug Type: *Session related
Assigned To: 
Comments:

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";
      }
    }


Previous Comments:
---------------------------------------------------------------------------

[2000-12-02 13:23:23] [EMAIL PROTECTED]
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!


---------------------------------------------------------------------------

[2000-11-27 08:52:26] [EMAIL PROTECTED]
Should be fixed in CVS. Please try latest snapshot from
http://snaps.php.net/ and reopen this bug report if
this doesn't work with it.

--Jani

---------------------------------------------------------------------------

[2000-11-03 19:32:58] [EMAIL PROTECTED]
works for me (4.0.4dev), please try 4.0.4 as far as it is
out and report results

---------------------------------------------------------------------------

[2000-11-02 03:09:55] [EMAIL PROTECTED]
Every call to the update function in the session class appends a new C_CART_ITEM with 
the actual time to the cart1 and cart2 arrays.

If you reload the page several times, you get the following:


   SESSION-ID: 970efca9d3effdadf14e679d0e88b349
   ITEMS:

   973150040 : 1 973150036 : 8
   : 1           973150038 : 7
   : 1           973150039 : 6
   : 1           973150039 : 5
   : 1           973150039 : 4
   : 1           973150040 : 3
   : 1           973150040 : 2
   : 1           973150040 : 1


The left two rows show the content of cart1 and the right 2 rows the content of cart2. 
Both arrays should have the same content.

cart1 is referenced by item in the show function. And when this is done, this entry is 
not available in the next page (after reload etc.).

cart2 is referenced as the whole array - this works.

This bug occurs with PHP versions > 4.0.3 (4.0.2 not tested).
It works fine with version 4.0.1pl1 .


---------------------------------------------------------------------------

[2000-10-31 18:00:18] [EMAIL PROTECTED]
<?
      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();
?>


---------------------------------------------------------------------------

The remainder of the comments for this report are too long.  To view the rest of the 
comments, please view the bug report online.


ATTENTION! Do NOT reply to this email!
To reply, use the web interface found at http://bugs.php.net/?id=7557&edit=2


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to