Reputation: 23
I've been researching a bit but I cannot find any way of doing this...
I need to pass an instance of a complex PHP class (formed by properties, functions, arrays, etc.) between different pages. The variable of the object is called $Model
; I've been trying to use the GET method and "serialize" / "unserialize", but I cannot retrieve the object in the new page...
I'm trying to pass the object as follows:
echo '<a href="index.php?modelobject='.serialize($Model).'">Pass Model Object</a>';
Then, I'm trying to retrieve it like this:
if(isset($_GET['modelobject'])) //$_GET when used in a link!
{
$ModelObjPost = unserialize($_GET['modelobject']); //$_GET when used in a link!
echo "POST: Model Object retrieved<br>";
var_dump($ModelObjPost);
}
There could be some kind of problem with certain characters in the serialization (I'm not sure, though), as the link that should send the object sometimes gets printed as (and it is also recognized as a URL):
r";s:3:"new";b:0;}i:2;(... MORE STUFF ...)s:9:"dbModelId";s:1:"1";}">Pass Model Object
Should I try a completely different approach, or this method should work, but there is just something I'm doing wrong?
Thanks in advance for your time!
Upvotes: 1
Views: 556
Reputation: 1822
Let's clear up some terminology right up front. I assume you know this, but just to be clear... A class is both the code that defines the behavior you want for an object and data (properties). An instance is when you use the new
keyword on a class to create a usable object using that class definition.
By the very nature of how PHP works (typically), all instances are unloaded and deleted after a page loads. It cannot survive in memory to be used on a second page.
Typically you would create a file that contains the class definition you're trying to pass between pages, and create a new instance of the class on each page.
If you're trying to keep state between pages, you should look at using sessions. If you choose to use sessions, and want to keep an instance of your class inside your session, keep in mind what I said above - it will be deleted and recreated between the pages. You will need to make sure your class is set up to reload everything it needs to operate. PHP provides for a "magic" method to do this: __wakeup()
in this method, you will need to restore the object back to the same state it was in on the previous page load.
Other ways to pass data between pages (or page loads) would be arrays for HTTP GET or POST.
$data = array( 1,2,3,4, 'a' => 'abcd' );
$query = http_build_query(array('aParam' => $data));
$url = 'somepage.php?data=' . $query;
Forms may be created to pass arrays of data by utilizing array notation in the form field names
<form action="somepage.php" method="post">
<input name="option[a][]" value="option a,0">
<input name="option[a][]" value="option a,1">
<input name="option[b][]" value="option b,0" />
<input name="option[b][]" value="option b,1" />
<input name="option[]" value="option 0" />
</form>
Access this data like this:
<?php
echo $option['a'][0];
echo $option['a'][1];
echo $option['b'][0];
// etc
Upvotes: 2
Reputation: 557
Your two best options in this case would be to either:
$_SESSION
variable. You could save the object as $_SESSION['Model']
and access that on each page that you need to use it. Be sure to call session_start();
on each of those pages to resume the session.urlencode(serialize($Model))
in the URL and use urldecode()
on the next page to make sure that you don't have encoding issues in the URL. json_encode()
and json_decode()
would also be a good option for object to string serialization.Upvotes: 0