Connecting Tech Pros Worldwide Help | Site Map

Saving Object in a session...

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 12:53 PM
Joe
Guest
 
Posts: n/a
Default Saving Object in a session...

Hi all,

I'm a java programmer learning PHP. I was wondering how to save a
class over a session. As a simple example, if I wanted to have a
Class called Sum, which had the methods addToSum($a), and printSum().
Could I save that object into a session, so that I could use it later
as the user navigates my webpage?

Is a session the best way to store data as a user goes between
webpages?

thanks,


  #2  
Old July 17th, 2005, 12:53 PM
Joshua Gao
Guest
 
Posts: n/a
Default Re: Saving Object in a session...

Joe wrote:[color=blue]
> Hi all,[/color]
Hi :)[color=blue]
>
> I'm a java programmer learning PHP. I was wondering how to save a
> class over a session. As a simple example, if I wanted to have a
> Class called Sum, which had the methods addToSum($a), and printSum().
> Could I save that object into a session, so that I could use it later
> as the user navigates my webpage?[/color]
Depending on your version of PHP, you could probably do so. It might be
advisable to serialize() the object, and unserialize() it when you use
the object, if you wish to store it to an SQL database later, or are
using an old version of PHP, but if you're doing something that simple,
it should work fine.[color=blue]
> Is a session the best way to store data as a user goes between
> webpages?[/color]
It is the easiest, and generally the fastest. Unless you need customized
options it should be fine :)

-Joshua Gao
  #3  
Old July 17th, 2005, 12:53 PM
\(¯`·..Yttrium ...·´¯\)
Guest
 
Posts: n/a
Default Re: Saving Object in a session...

"Joe" <sniff@email.arizona.edu> a écrit dans le message de news:
1114648910.097437.250930@o13g2000cwo.googlegroups. com...[color=blue]
> Hi all,
>
> I'm a java programmer learning PHP. I was wondering how to save a
> class over a session. As a simple example, if I wanted to have a
> Class called Sum, which had the methods addToSum($a), and printSum().
> Could I save that object into a session, so that I could use it later
> as the user navigates my webpage?
>
> Is a session the best way to store data as a user goes between
> webpages?
>
> thanks,[/color]


Hi,
No problem, you can do that easily.
Sessions are made for this .. :-)


  #4  
Old July 17th, 2005, 12:53 PM
Peter Fox
Guest
 
Posts: n/a
Default Re: Saving Object in a session...

Following on from (¯`·..Yttrium ...·´¯)'s message. . .[color=blue]
>"Joe" <sniff@email.arizona.edu> a écrit dans le message de news:
>1114648910.097437.250930@o13g2000cwo.googlegroups .com...[color=green]
>> Hi all,
>>
>> I'm a java programmer learning PHP. I was wondering how to save a
>> class over a session. As a simple example, if I wanted to have a
>> Class called Sum, which had the methods addToSum($a), and printSum().
>> Could I save that object into a session, so that I could use it later
>> as the user navigates my webpage?
>>
>> Is a session the best way to store data as a user goes between
>> webpages?
>>
>> thanks,[/color]
>
>
>Hi,
>No problem, you can do that easily.
>Sessions are made for this .. :-)
>
>[/color]
But watch out for the way PHP copies/assigns variables.

This works fine
$adder = new myAdder(0);
$adder->plus(5);
$adder->plus(7);
print($adder->total()); // 12

But this doesn't (may not)
$adder = new myAdder(0);
$adder->plus(5);
$_SESSION['ADDER']=$adder;
$adder->plus(7);
print($adder->total()); // 12
print($_SESSION['ADDER']->total()); // 5 Is this what you wanted?

But this is the way to do it
$_SESSION['ADDER']= & $adder; //<<<<<<< &



--
PETER FOX Not the same since the submarine business went under
peterfox@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
  #5  
Old July 17th, 2005, 12:53 PM
Peter Fox
Guest
 
Posts: n/a
Default Re: Saving Object in a session...

Following on from Joe's message. . .

My last post in this thread was off the top of my head.
The & is used when getting access to the session rather than putting it
in in the first place.


Here is a code snippet which appears in many pages.
database is a home-grown class and references to session database are
similar.
It is the last line which says "$db will refer to the object in the
session"


Sorry about the earlier confusion.

// get database connection
if(!DoesSessionDatabaseExist()){
$db = new database('localhost','sukuser','NOTTHEPASSWORD','s ukas');
$x=$db->Connect();
if($x){
print("<p>Error connecting to database<br>$x<p>");
exit;
}
PutDatabaseInSession($db);
}
$db = & SessionDatabase();
--
PETER FOX Not the same since the submarine business went under
peterfox@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.