PHP 5 Classes and Objects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pppswing
Forum Commoner
Posts: 33
Joined: Thu Jun 10, 2004 2:04 am
Location: Tallinn, Estonia

PHP 5 Classes and Objects

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: PHP 5 Classes and Objects

Post 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)..
pppswing
Forum Commoner
Posts: 33
Joined: Thu Jun 10, 2004 2:04 am
Location: Tallinn, Estonia

Post 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 ?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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);

?>
Post Reply