This article mainly introduced in PHP to save an object to the session of the method, involving PHP operating objects and session skills, with a certain reference value, the need for friends can refer to the
-->
This example describes how to save an object in the session in PHP. Share to everyone for your reference. Specifically 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 is as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20 |
<?php////file:person_class.inc.php//contains the class definition necessary to let an object is a session//VARIABL E.//class Person {var $name; var $email;////A simple function to illustrate the point//function Clean_name () {$n Ame = Preg_replace ("/h" (.) +/i "," 1 ", $this->name); Return substr ($name, 0, 15); }}?> |
main.php files are as follows:
?
1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 |
<?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> |
sompage.php files are as follows:
?
1 2 3 4 5 6 7 8 9 10 |
<?php////file:somepage.php//print out the name without initializing the//class and setting the variables//includ E_once ' person_class.inc.php '; Session_register (' Foobar '); Print $foobar->name;?> |
I hope this article will help you with your PHP programming.