php将一个对象保存到Session功能实例

前端之家收集整理的这篇文章主要介绍了php将一个对象保存到Session功能实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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)   ***/
原文链接:https://www.f2er.com/php/528333.html

猜你在找的PHP相关文章