This article mainly introduces the method of saving an object to the session in PHP, involving PHP operation object and session skills, has a certain reference value, the need for friends can refer to the next
The example in this article describes how to save an object to the session in PHP. Specific as follows:
To save the object to the session is actually very simple, we can use the Session_register () function, the following is the use of the example
person_class.inc.php as follows:
<?php////file:person_class.inc.php//contains the class definition necessary to let an object is 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, 0,); }? >
The main.php file is as follows:
<?php////file:main.php//here is where we save and retrieve the object//include_once ' person_class.inc.php '; 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>
The sompage.php file is as follows:
<?php////file:somepage.php//print out the name without initializing The//class and setting the Variables//include_onc E ' person_class.inc.php '; Session_register (' Foobar ');p rint $foobar->name;? >
Summary : The above is the entire content of this article, I hope to be able to help you learn.