Re: [PHPLIB] registering a class? From: Kristian Köhntopp (kris <email protected>)
Date: 12/17/99

Phill Kenoyer wrote:
> If I setup a class, could I register that class to save all the parameters
> vars?

You cannot make a class persistent, because it already is
persistent in your .inc file. You can make objects, instances
of that class, persistent.

To make an object persistent the following things must be
true:

a) the class must have a slot named $classname and that slot
   must be the exact name of that class. PHPLIB will internally
   do

   $obj = new $classname.

b) the class must have a slot named $persistent_slots and that
   slot must be an array of string enumerating the names of all
   slots that are to be saved.

An example would be

/* example_class.inc */
class Example_Class {
        var $classname = "Example_Class";
        var $persistent_slots = array("a", "b", "c");

        var $a, $b, $c;
        var $d, $e, $f;

        /* Constructor */
        function Example_Class($o, $p, $q) {
                $this->a = $o;
                $this->d = $o;

                $this->b = $p;
                $this->e = $p;

                $this->c = $q;
                $this->f = $q;
        }
}

and on your page

page_open(array("sess" => "Example_Session"));

$obj = new Example_Class(1, 3.14, "Hello World");
$sess->register("obj");

page_close();

The class Example_Class is already persistent, as its definition
is available in the file example_class.inc and that never changes.

The variable $obj is an instance of Example_Class, an object.
Since Example_Class covers all prerequisites required by PHPLIB
to be made persistent, $obj can be made persistent using a call
to Session::register(). As defined, $obj->a, $obj->b and $obj->c
will be saved and $obj->d, $obj->e and $obj->f won't.

Please note that on all following pages, $obj will be part of
the session until you unregister it. That implies that PHPLIB
will execute $obj = new Example_Class on each following page.
This particular statement has only meaning if PHP has previously
seen a definition of Example_Class on that page. That is, you
must include example_class.inc on each of these pages as long
as $obj is part of your session.

Since you cannot know in advance which pages a user will vistit
and in what order, this effectively means that you must require()
the file example_class.inc in your prepend.php3.

Kristian

-- 
Kristian Koehntopp, Knooper Weg 46, 24103 Kiel, +49 171 2231 811
"Da kann Deine Nummer 1 naemlich ganz lieblich Fruehstueck machen.
 Und Fruehstueck, ey, da stehst Du doch auf."

- PHP3 Base Library Mailing List. Send messages to <phplib <email protected>>. To unsubscribe, send "unsubscribe" to <phplib-request <email protected>> in the body, not the subject, of your message. - PHP3 Base Library Mailing List. Send messages to <phplib <email protected>>. To unsubscribe, send "unsubscribe" to <phplib-request <email protected>> in the body, not the subject, of your message.