How to save an object in the session in PHP

Source: Internet
Author: User
Tags class definition php programming

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.