Jaison Justus
Jaison Justus

Reputation: 2793

How to transfer object from one php file to another php file

i have a query that, how can i transfer an object from php file A to php file B?. but i know a solution using session.But what i need to know is, is their any other method to transfer object between php files other than session?

Upvotes: 0

Views: 2867

Answers (7)

m3hr24d
m3hr24d

Reputation: 121

you can use this method: serialize() and unserialize()

fileA.php :

<?php
require_once 'employee.class.php';
$employee=new employee($id,$firstname,$lastname);
$serializeemployee=serialize($employee);
session_start();
$_SESSION['employee']=$serializeemployee;
header('location: ./fileB.php');
?>

fileB.php :

<?php
session_start();
if(isset($_SESSION['employee']) && $_SESSION['employee'])
{
require_once 'employee.class.php';
$employee=unserialize($_SESSION['employee']);
echo $employee->getFirstname();
?>

Upvotes: 1

Kieran Allen
Kieran Allen

Reputation: 942

APC is probabaly the easiest method:

example:

// new object
$object = new ClassName('Kieran', 123);

// Store it
apc_store('object', $object); 

The other script

$obj = apc_fetch('object');
print_r($obj->method());

Upvotes: 1

NikiC
NikiC

Reputation: 101936

The easiest way (besides using sessions) is probably to save it as an APC (Alternative PHP Cache) user variable, as you probably will install APC for opcode caching purposes already. This way you have one extension for two things.

APC stores values inmemory as Memcached does, but is way easier to install, because it's not an extra daemon running, but a PHP extension.

Upvotes: 0

Prabhu Murthi
Prabhu Murthi

Reputation: 2876

by using cURL you can transfer any thing... try this..

http://php.net/manual/en/book.curl.php

Upvotes: 0

RobertPitt
RobertPitt

Reputation: 57268

Serialize the object and store in one of the following (Database,Temp,Memcache).

Depending on what the object consists of i would take a look at implementing the __sleep and __wake magic methods to make sure the object is able to be transferred correctly.

Upvotes: 0

Pekka
Pekka

Reputation: 449435

One way is to store the serialized object - or its data - into a database, using the session ID as the key to "find" it again.

The same could be done using a cache file.

A faster way is using a shared memory cache like memcache. These solutions always require server-side administration and root access to set up.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798666

Save in a file, save in a database, save in shared memory, save in a cache server.

Upvotes: 1

Related Questions