0

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?

1

7 Answers 7

1

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());
Sign up to request clarification or add additional context in comments.

3 Comments

is APC avail in all hosting servers... or we need to install or some do something else
Check phpinfo(); If it isn't i recommend installing it.
Itrs abailable in PHP 5.3, You just have to enable it in your php.ini under apc.dll If it does not exists you can get it here pecl.php.net/package/apc
1

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

4 Comments

can you explain about saving in shared memory and saving in a cache server.
Using memcached.. "Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects)"
Bear in mind Memcached for a single server setup is pointless (has a tcp/ip overhead)
1

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.

Comments

1

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();
?>

Comments

0

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.

Comments

0

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

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

3 Comments

I dont think the OP Means by Cross Server requests, Who would want to send objects across HTTP Anyway?
@RobertPitt: By first serializing them, for example.
objects can be very huge, i know you can serialize them before sending them, but there still can be very very large and will sometimes override the Apaches HTTP_VARS Max allowed char etc, i personally would store the file locally, ping the server with an address of the temp file, then use curl to get the file, and then ping back to the server to delete.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.