Page 1 of 1

PHP 5 Classes and Objects

Posted: Fri Oct 28, 2005 6:23 am
by pppswing
Hello,

I got question about classes in PHP5,
For example I have developed 2 classes MyClass1 and MyClass2
in files MyClass1.php and MyClass2.php

Then I want to use them in file index.php, next.php and close.php

Suppose that users are connecting on page index.php,
And I create new MyClass1Obj and MyClass2Obj in index when the connexion is set

what is the life time of Objects MyClass1Obj and MyClass2Obj ?
During all the session of one user ?
Is it shared between differents users ?
Can I destroy the object myself using MyClass1Obj.__destruct() ?

I would like to create some objects that are available :
for only one user and during all the session.
for everybody, reusable between user and session.


How can I do that ? :roll:

Thanx

Re: PHP 5 Classes and Objects

Posted: Fri Oct 28, 2005 6:35 am
by feyd
pppswing wrote:what is the life time of Objects MyClass1Obj and MyClass2Obj ?
During all the session of one user ?
that page request unless you are using object streaming or other trickery..
pppswing wrote:Is it shared between differents users ?
nope, unless you are using shared memory systems..
pppswing wrote:Can I destroy the object myself using MyClass1Obj.__destruct() ?
Technically, that won't destroy the object. Only php has the ability to delete the object. You can only ask php to remove it. Whether it actually does or not is up to it.
pppswing wrote:I would like to create some objects that are available :
for only one user and during all the session.
for everybody, reusable between user and session.


How can I do that ?
store the object in their session information. Just remember that you must have the class defined before starting the session again (on subsequent pages)..

Posted: Fri Oct 28, 2005 7:43 am
by pppswing
When can I create object and How can I store object on session information (to make object available for one session) ?
How can I use Object streaming to share object between different session ?

I must put __autoload() in the begining of every file to declare classes, right ?

Posted: Fri Oct 28, 2005 7:48 am
by Chris Corbyn
__autoload() is not a requirement but it does save a lot of coding.

feyd means that you can physically store the instance of the object you create within the $_SESSION superglobal and then pull it back out of the session on each page request. I've never tested this, and I expect with PHP4 it would cerainly have just been a new instance of the object but in PHP5 your object may be preserved in this way.

Test it :D

Posted: Fri Oct 28, 2005 8:59 am
by Jenk
I've seen both the following methods:

Code: Select all

<?php
session_start();

$_SESSION['Obj'] = new myClass();

//script

?>

<?php
session_start();
$Obj = unserialize($_SESSION['Obj']);


//script

$_SESSION['Obj] = serialize($Obj);

?>