A typical example of passing objects with the session, shared with friends with object-oriented programming (from CSDN)

Source: Internet
Author: User
Tags date constructor include interface connect php and variable access
Session| Programming | Object First of all, your session to be so, PHP4.1 above version don't forget to PHP.ini Register_globle=off set to Register_globle=on, There is Session.cookie_path =/, note that this line can not be changed, and some version of PHP changed this setting session is not good, this is a bug in PHP.
Well, I assume your session is ready, and I'll explain the following example: B.php and a.php each define a class in which class A in a.php is slightly more complex because it is made up of the class doc in b.php, This is called the composition relationship of the class. My example is probably the simplest example of a class composition relationship.
C.php produces a Class A object called test and registers it as a session variable, and c.php also displays a hypertext form interface for you to enter a string. After clicking the button, Object test is passed to the d.php, in the following program, I specifically described the things to be aware of when passing objects. This program can actually run, it is I learn to pass the object with the session a small summary, we can go back to try, I wish you happy.
?
a.php
Include ("b.php");//contains b.php because the Doc class is defined in b.php, and the Doc property in Class A is an object of the Doc class
/**
* Short description.
* A class with a constituent relationship
* Detail Description
* @author
* @version 1.0
* @copyright
* @access Public
*/
Class A
{

/**
* Description This is a simple variable as a property of Class A
* @var
* @since 1.0
* @access Private
*/
var $docid;

/**
* Description This is an object variable as a property of Class A
* @var
* @since 1.0
* @access Private
*/
var $doc;


/**
* Short description.
* Constructor
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function A ()//Class A constructor, which assigns its own Doc property an initial value
{
$adoc =new Doc;
$this->doc= $adoc;
}//End Func
/**
* Short description.
* Assigning value to DocId
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function getdocid ()//It assigns a value to its DocId property
{
$this->docid= $this->doc->id;
}//End Func

/**
* Short description.
* This function is very simple, it does not explain
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function printdocid ()//Displays the id attribute of the Doc object property
{
echo "doc->id=". $this->doc->id. " <br> ";

}//End Func

}//End Class
?>
?
b.php
/**
* Short description.
* This is a simple class
* Detail Description
* @author
* @version 1.0
* @copyright
* @access Public
*/
Class doc
{

/**
* Description Simple variable as the property of the class doc
* @var
* @since 1.0
* @access Private
*/
var $id;



/**
* Short description.
* This function displays a input interface
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
Function SCR ()
{
Screen ();
}//End Func
/**
* Short description.
* This function puts the value entered in the above into the library
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function Save ($conn, $i)
{
$sql = "INSERT into ' test ' (' id ', ' name ') VALUES (', ' $i ')";
$result =mysql_query ($sql, $conn);
$this->id = mysql_insert_id ($conn);
}//End Func
}//End Class
? ><?
c.php
Include ("a.php");//The object to produce Class A must include the file that defines Class A (note that a.php cannot have hypertext, a single space is not available, because Session_Start has the previous requirement, and cannot contain hypertext files. If the feature requirement must contain hypertext, make a function like the following screen.php
$test =new A ();//Create an object of Class A, named Test
Session_Start ();
Session_register (' test ');//Register object variable as session variable to pass
Include ("screen.php"), and/or a file with hypertext
$test-&GT;DOC-&GT;SCR ()//Call interface display

? ><?
d.php
Include ("a.php"); To accept the previously passed object variable with session, you must include a file that defines the class before Session_Start ()
Session_Start ();
Include ("conn.php");//The definition of the Connect () function used in the following sentence is defined in this file
$conn =connect ();
echo "i= $i <br>";
$test->doc->save ($conn, $i);//This Test object is established in c.php, and passed through the session, it is the purpose of this program display, please pay attention to experience; The properties and methods of the test object are still valid after the session is passed
$test->getdocid ();
$test->printdocid ();
?>
<title>untitled document</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body bgcolor= "#FFFFFF" text= "#000000" >
<a href= "c.php" >return</a>
</body>
?
conn.php

/**
* Short description.
* This is a common MySQL connection function, nothing special.
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
Function Connect ()
{$conn =mysql_connect ("localhost", "", "");
mysql_select_db ("Test", $conn);
return $conn;
}//End Func

?>
?
screen.php
/**
* Short description.
* The reason for these hypertext to be used as a function is to not include hypertext in other PHP files, to achieve the purpose of separating the PHP program from the hyper text.
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function screen ()
{
?>
<form method=post action= "d.php" >
<input type= "text" name= "I" >
<input type= "Submit" >
</FORM>
?
}//End Func
?>
Hopefully the above example will be added to the FAQ, and I won't have to keep this program on my own ^_^

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.