Jump to content


Photo

Passing Objects between php files


  • Please log in to reply
1 reply to this topic

#1 jed3

jed3
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 23 October 2006 - 09:35 PM

Hello,

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

what I have is similar to this...

<?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>

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?

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 23 October 2006 - 09:57 PM

You need to serialize your object, and the easiest way is to use sessions. eg;

obj.php

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

p1.php

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

p2.php

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users