[PHP] Are objects considered global or do they need registered in a session?

2002-11-19 Thread Phil Schwarzmann
When declaring a new object ( $obj=new Something; ), do they need to be
then registered in a session ( session_register(obj); ) if you want to
use them for a different script?



Re: [PHP] Are objects considered global or do they need registered in a session?

2002-11-19 Thread Ernest E Vogelsinger
At 16:10 19.11.2002, Phil Schwarzmann spoke out and said:
[snip]
When declaring a new object ( $obj=new Something; ), do they need to be
then registered in a session ( session_register(obj); ) if you want to
use them for a different script?
[snip] 

Phil,

you use an object just as any other variable. You can pass the object
(preferrably by reference) as function parameter, make the object reference
global (e.g. by putting it into the $_GLOBALS array), store it to
$_SESSION, hand it to print_r(), whatever you want to do with it.

Regarding a session: you may always store an object reference as a session
variable. However, before restoring the object instance in start_session()
the necessary implementation classes need to be defined.

Example:

// this will work
class A {
function A() {
}
}
session_start();
if (!is_object($ref_a)) {
$ref_a = new A();
$_SESSION['ref_a'] = $ref_a;
}

// this will NOT work when reloading the session!
session_start();
class A {
function A() {
}
}
if (!is_object($ref_a)) {
$ref_a = new A();
$_SESSION['ref_a'] = $ref_a;
}

Hope this helps,

-- 
   O Ernest E. Vogelsinger 
   (\) ICQ #13394035 
^ http://www.vogelsinger.at/