The PHP implementation saves the object to the session

Source: Internet
Author: User
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.

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.