Jump to content

Archived

This topic is now archived and is closed to further replies.

jed3

Passing Objects between php files

Recommended Posts

Hello,

I would like to know how I might pass an Object between files?

what I have is similar to this...

[code]
<?php
if($_GET['mode'] == 'send') {
$email = $_GET['obj'];
$email->sendMail();
}

$email = new EmailObject("to", "subject", "body");

?>

<form action="email.php?mode=send&obj=<?php echo $email; ?>">
<input type="submit" value="submit">
</form>
[/code]

I am aware this is wrong, I just wanted to get my question across as straightforward as possible.  I was trying to avoid having to use serialization, but if that would be the only method what would be the easiest way?

Share this post


Link to post
Share on other sites
You need to serialize your object, and the easiest way is to use sessions. eg;

obj.php

[code=php:0]
<?php
  class foo {
    private $bar
    function __construct($var) {
      $this->bar = $var;
    }
    function getBar() {
      return "this is ".$this->bar;
    }
  }
?>
[/code]

p1.php

[code=php:0]
<?php
  session_start();
  include 'obj.php';
  $foo = new foo('a test');
  $_SESSION['obj'] = $foo;
  echo "<a href='p2.php'>p2</a>";
?>
[/code]

p2.php

[code=php:0]
<?php
  session_start();
  include 'obj.php';
  $foo = $_SESSION['obj'];
  echo $foo->getBar();
?>
[/code]

Share this post


Link to post
Share on other sites

×

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.