note 31349 deleted from language.oop by danbrown
| From: | danbrown@php.net | Date: | Sat, 07 Nov 2009 19:04:08 +0000 |
| Subject: | note 31349 deleted from language.oop by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162572@lists.php.net to get a copy of this message | ||
Note Submitter: russ dot taylor at nifty dot com
----
It is sometimes useful to register a class instance as a session variable. This can allow classes
and their contained values to persist across multiple pages and/or frames. Frames which need to use
the registered class must be loaded (or reloaded) after the class instance is registered and the
class declaration must be included in each file that needs to access the registered class.
i.e.
<?
session_start();// must have a session, duh...
class foo{//normal class declaration
var $bar;
function foo(){//normal constructor
$this->bar = "a value to share and persist";
}
}
$vital = new foo;//instance "vital" of object type "foo"
session_register("vital");//register the instance "vital"
//other code as needed
?>
**NOTE: it would be helpful to include the class from an external source so the same declaration
could be used everywhere its needed, duh
then simply include the class declaration in any other file and you will have access to the
registered class instance. in a separate frame on the same page you could use ...
<?
session_start();
class foo{//same class declaration
var $bar;
function foo(){//normal constructor
$this->bar = "a value to share and persist";
}
}
echo $vital->bar;
?>
**NOTE: if you use a frameset you must allow processing time of the session registration or the
value will not be available! You can use any number of methods to trigger the delay, but the
session must NOT be started on the second page beforre the variable is registered on the first. On
option is to use an onload statement in the first frames body tag, assuming normal html output...
<html><head></head><body
onLoad="parent.frame[1].location.reload();"><!--what ever else you need to
output--></body></html>
"frame[1]" assumes a two frame layout with frame[0] containing the values to share and
frame[1] requiring those values. thus this, when included in frame[0] will force a reload of
frame[1] AFTER the class instance was registered.