Re: [PHP-PEAR] Object persistence

[email protected] ("Paul Meagher")
Newsgroups php.pear
Message-ID <001a01c09109$a909d160$6c35de18@datavore>
Thanks Vinai,

This works!  I am not sure what I was doing wrong.  Sorry for wasting
bandwidth on this issue.

FYI, here are my 3 test scripts: a Test class (Test.inc), a page where a
Test instance is created, registered, and attributes echoed (page1.php),
and a subsequent page (page2.php) where the attributes are again echoed
(without being created and registered).

The scripts demonstrate that once you register an object to a session, on
subsequent pages you can access the scalar values, array values, and
methods associated with that object.

I am using PHP  4.0.4pl1

<?php

// Script Name: Test.inc

class Test {
  var $auth_table      = 'access_control';
  var $access_levels   = array();

  function Test () {
    $this->access_levels['student']  = 1;
    $this->access_levels['admin'] = 2;
  }

  function printAccessLevel($user_type) {
    echo $this->access_levels[$user_type];
  }
}

?>

<?php

// Script Name: page1.php

require_once "Test.inc";

session_start();
session_register('test');

$test = new Test;

echo "Access control table: $test->auth_table <BR>";

echo "Access Level for Students: ". $test->access_levels['student']
."<BR>";

echo "Print Access Level for Admin: ";
$test->printAccessLevel("admin");
echo "<BR>";

echo "<a href='page2.php'>Next Page</a>";

?>

<?php
// Script Name: page2.php

require_once "Test.inc";

session_start();

echo "Access control table: $test->auth_table <BR>";

echo "Access Level for Students: ". $test->access_levels['student']
."<BR>";

echo "Print Access Level for Admin: ";
$test->printAccessLevel("admin");
echo "<BR>";

?>



----- Original Message -----
From: "Vinai Kopp" <[email protected]>
To: "Paul Meagher" <[email protected]>
Cc: <[email protected]>
Sent: Wednesday, February 07, 2001 2:42 AM
Subject: Re: [PHP-PEAR] Object persistence


> I have no problems registering objects as session-variables as long as I
> include the classes for every object I use *before* I call
> session_start().
>
> HTH,
>
> Vinai
>
> Paul Meagher wrote:
> >
> > I have been experimenting a bit with trying to register and pass
objects
> > from one page to the next.  PHP's support for this appears to be quite
> > limited and possibly fickle.  My search throught the mail archives
reveals
> > similiar gripes about its lack of support in this area.
> >
> > It could be that I just don't know the proper way to pass such objects.
I
> > would like to know, however, if other people have run into similiar
> > difficulties and if there is a "best practices" way of doing this.
> >
> > Assuming that there are limitations to PHP's current support for object
> > persistence, I am wondering if session management for objects is
something
> > that should be / is going to be implemented at a language level or
whether
> > it should be implemented as a PEAR object persistence class of some
sort?
> >
> > Regards,
> > Paul Meagher
>
> --
> PHP Extension and Add-on Repository (PEAR) mailing list.
> Documentation can be found at http://pear.php.net/doc/pear.html
> To unsubscribe, e-mail: [email protected] from the
> mail address you subscribed with.
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.