Jump to content

Recommended Posts

How do I access an object on different pages?

 

I created object "A" on pageone.php and I need to access it on pagetwo.php.

 

How do I do that?

 

 

TomTees

 

 

You'd have to pass it through sessions.

 

You can pass an entire Object through a Session??  :o

 

Is this safe?    :-\

 

Any "gotchas"?

 

Could you give me a snippet showing me how to do this?

 

Let's say I have...

 

class Ticket{

  private $price;

  private $destination;

}

 

$objTicket = new Ticket('$350', 'Miami');

 

on Booking.php and I need to have access to the object on RegistrationSummary.php

 

How would I do that using a Session??

 

Thanks,

 

 

TomTees

 

 

$_SESSION['objTicket'] = new Ticket('$350', 'Miami');

 

Don't forget that your class definition needs to exist before the session_start() statement so that the object can be recreated from the session data file.

$_SESSION['objTicket'] = new Ticket('$350', 'Miami');

 

Don't forget that your class definition needs to exist before the session_start() statement so that the object can be recreated from the session data file.

 

Hmm...  That is strange code?!

 

Why don't you declare the class and instantiate it using normal syntax and then assign it to a session?

 

<?php

  class Ticket{

    $private price;

    $private destination;

}

 

$objTicket = new Ticket('$350', 'Miami');

?>$_SESSION['objTicket']

 

 

TomTees

 

 

Why don't you declare the class and instantiate it using normal syntax and then assign it to a session?

 

Because it's the same.

 

class SomeClass {
  private $data = array('hello', 'world');
  
  function __toString() {
    return serialize($this);
  }
  
  function __sleep() {
    return array('data');
  }
}

 

$c = new SomeClass();
echo serialize($c), ' ', $c, "<br>\n";

$s = serialize($c);
$d = strval($c);
var_dump(unserialize($s));
var_dump(unserialize($d));

 

Why don't you declare the class and instantiate it using normal syntax and then assign it to a session?

 

 

Because it's the same.

 

 

class SomeClass {  private $data = array('hello', 'world');    function __toString() {    return serialize($this);  }    function __sleep() {    return array('data');  }}

 

 

 

$c = new SomeClass();echo serialize($c), ' ', $c, "<br>\n";$s = serialize($c);$d = strval($c);var_dump(unserialize($s));var_dump(unserialize($d));

 

 

 

 

You guys are losing me here...

 

I was under the impression that if I store an Object in a Session then that is all I need to do (and then I do NOT have to Serialize the Object as well).

 

Is that correct?

 

I thought that is what the PHP manual said.

 

Also, what is the fascination with Serialization by some people?

 

From what I have read, Serialization is so "old school" and just using Sessions is better.  :shrug:

 

 

 

TomTees

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.