How can I register a class in $sess?

Help
2005-11-20
2013-04-09
  • Could you give a complete example of what needs to be done to register a class in $sess?

    Let's say I have

    class.foo.php:
    <?
    Class foo {
      var $classname;
      var $persistent_slots;
      var $x;

      function foo() {
        $this->classname = 'foo';
        $this->persistent_slots = array('x', 'y');
        $x = 1;
      }
    }
    ?>

    then I have:

    login.php:
    <?php
      require_once('prepend.php3');
      page_open(array('sess' => 'X_Session', 'auth' => 'X_Challenge_Crypt_Auth', 'perm' => 'X_Perm', 'user' => 'X_User'));
      $auth->login_if($again && ($auth->auth['uid'] == 'nobody'));

      require_once('class.foo.php');
        $foo =& new foo;

      $sess->register('foo');
      page_close();
      header('location:test.php');
    ?>

    finally, on test.php I have:

    <?php
      require_once('prepend.php3');
      page_open(array('sess' => 'X_Session', 'auth' => 'X_Challenge_Crypt_Auth', 'perm' => 'X_Perm', 'user' => 'X_User'));
      $auth->login_if($again && ($auth->auth['uid'] == 'nobody'));

      require_once('class.foo.php');
        $foo =& new foo;

        echo $foo->x;
      page_close();
    ?>

    However, I get the following error:
    Fatal error: Cannot instantiate non-existent class: foo in c:\phpdev\inc\session.inc(361) : eval()'d code on line 1

    I tried defining $this->persistent_slots as

    array('x');
    array('x'=>'x);
    array('classname', 'persistent_slots', 'x');
    array('classname'=>'classname', 'persistent_slots'=>'persistent_slots', 'x'=>'x');

    all with no luck.

    What am I missing?

    Thanks in advance,
       Christopher Vrooman.

     
    • Richard Archer
      Richard Archer
      2005-11-20

      In test.php, when you load up the session (page_open) it declares or instantiates your session variables.

      So you need to require_once class.foo.php before your page_open. And then you don't want to $foo= later on because $foo will have already been set when the session loaded.