I have some unclear (to me) behaviour in using classes and references to 
session.
I submit you this code:

<?php
//some dummy classes
class A{
  var $score;

  function A(){
    $this->pippo = "pluto";
  }
  function echoname(){
    echo (get_class($this)."\n");
  }

}

class B {
  var $score;
  function B(){
    //assigning by value cause istances 
    //to be istances of a (same of $_SESSION[recorded])
    //but non reference is made, so it's a copy
    $this = $_SESSION["recorded"];
  }

  function echoname(){
    echo (get_class($this)."\n");
  }

}

class C {
  var $score;
  function C(){
    //assigning by reference cause istances 
    //to be istances of c and not the same of $_SESSION[recorded]
    $this = &$_SESSION["recorded"];
  }

  function echoname(){
    echo (get_class($this)."\n");
  }

}

session_start();
//new object stored in session.
$_SESSION["recorded"] =& new A();

echo "<pre>";
echo "session[\"recorded\"] classname:  ";
$_SESSION["recorded"]->echoname();
$_SESSION["recorded"]->score = 99;

echo "test_var classname:  ";
$test_var = new B();
$test_var->echoname();
$test_var->score = 100;

echo "test_var2 classname:  ";
$test_var2 = new C();
$test_var2->echoname();
$test_var2->score = 200;

print_r($test_var);
print_r($test_var2);
print_r($_SESSION);
echo "</pre>";
?>

This three classes ar equal, except in the constructor. The problem is 
explained in the comments, but essentially is this:
How do I create an istance of a class that has is a reference to another 
istance previuously stored in session?
During this day of coding I finally (I hope) wrote a test that explains what 
I can't do.

I really appreciate any help.

   gianpaolo

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to