对php中如何将一个对象保存到Session中的代码感兴趣的小伙伴,下面一起跟随脚本之家 jb51.cc的小编两巴掌来看看吧! php中如何将一个对象保存到Session中的代码要保存对象到session其实很简单,我们可以使用session_register()函数,下面是使用范例
person_class.inc:
/**
* php中如何将一个对象保存到Session中的代码
*
* @param
* @arrange 512-笔记网: 512Pic.com
**/
// File: person_class.inc
// Contains the class definition necessary to let an object be a session
// variable.
//
class Person
{
var $name;
var $email;
//
// A simple function to illustrate the point
//
function clean_name ()
{
$name = preg_replace("/h(.)+/i","1",$this->name);
return substr($name,15);
}
}
/*** 来自脚本之家 jb51.cc(jb51.cc) ***/ main.php:
/**
* php中如何将一个对象保存到Session中的代码
*
* @param
* @arrange 512-笔记网: 512Pic.com
**/
// File: main.php
// Here is where we save and retrieve the object
//
include_once 'person_class.inc';
session_register('someperson');
if (!$someperson) {
$someperson = new Foo;
$someperson->name = "Item Raja";
$someperson->email = "itemraja@php.net";
$someperson->clean_name();
}
<a href="somePage.php">Click Here</a>
/*** 来自脚本之家 jb51.cc(jb51.cc) ***/ somPage.php
/**
* php中如何将一个对象保存到Session中的代码
*
* @param
* @arrange 512-笔记网: 512Pic.com
// File: somePage.php
// Print out the name without initializing the
// class and setting the variables
**/
include_once 'person_class.inc';
session_register('foobar');
print $foobar->name;
/*** 来自脚本之家 jb51.cc(jb51.cc) ***/ (编辑:安卓应用网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|