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
https://forums.phpfreaks.com/topic/24877-passing-objects-between-php-files/
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]

Archived

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

×
×
  • 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.