Jump to content

Passing Objects between php files


jed3

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?
Link to comment
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]
Link to comment
Share on other sites

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.