通过PHP传递复杂对象

时间:2018-01-15 17:42:59

标签: php class serialization get

我一直在研究,但我找不到任何方法这样做......

我需要在不同页面之间传递复杂PHP类的实例(由属性,函数,数组等组成)。对象的变量称为$Model; 我一直在尝试使用GET方法和"序列化" /"反序列化" ,但我无法在新页面中检索对象 ...

我试图按如下方式传递对象:

echo '<a href="index.php?modelobject='.serialize($Model).'">Pass Model Object</a>';

然后,我试图像这样检索它:

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

序列化中的某些字符可能存在某种问题(我不确定),因为应该发送对象的链接有时会被打印为(并且它也被识别为URL):

r";s:3:"new";b:0;}i:2;(... MORE STUFF ...)s:9:"dbModelId";s:1:"1";}">Pass Model Object

我应该尝试一种完全不同的方法,或者这种方法应该有效,但我只是做错了吗?

提前感谢您的时间!

2 个答案:

答案 0 :(得分:2)

Warning: I would discourage serializing a class and placing the result in an end-user readable location. This probably reveals far more about your application than you should be disclosing publicly.


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

答案 1 :(得分:0)

Your two best options in this case would be to either:

  1. Use a $_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.
  2. Use 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.
相关问题