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?
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());
Save in a file, save in a database, save in shared memory, save in a cache server.
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.
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();
?>
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.
by using cURL you can transfer any thing... try this..
http://php.net/manual/en/book.curl.php
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.
精彩评论