Php: use serialization or session object to make oo php support object-oriented programming. This allows us to use classes and objects as much as possible during programming to simplify our program, reduces the workload of code maintenance. however, because the variables are released after the script is executed, the objects generated on this page may be in trouble when they are used on other pages. however, in many cases, some tasks are php: serialization or session object is used to make oo to the end.
Php supports object-oriented programming, which allows us to use classes and objects to simplify programming.
Our programs reduce the workload of code maintenance.
However, because the variables are released after the script is executed, the objects generated on this page want to be used on other pages.
However, in many cases, some tasks cannot be completed on one or two pages.
It is not a good idea to pass the object and its method to the page where we want to use the object.
Although the get or post hide method is used to pass object variables, a new object is created on the new page, and then
It seems that the new object can be initialized using constructors, but it is very troublesome. you can imagine it.
After a long attempt, we will summarize two methods to pass objects.
One, serialization (php4 features). With this feature, we can serialize the object, and then we need
To use the object variable deserialization.
Example:
// ********* Class. php definition class *********
Class
{
Var $;
Var $ B;
Var $ c;
Function a ($ x, $ y)
{
$ This-> a = $ x;
$ This-> B = $ y;
$ This-> give_var_value ();
}
Function give_var_value ()
{
For ($ I = 0; I I <100; $ I ++)
{
$ This-> c [$ I] = $ I;
}
}
Function show_var_value ()
{
Echo $ this-> a. $ this-> B ."
";
For ($ I = 0; I I <100; $ I ++)
{
Echo "c [$ I] =". $ this-> c [$ I]."
";
}
}
}
?>
// ************** A. php creates an object and serializes it **************
Require_once ("class. php ");
$ A = new a ("hello,", "world! ");
$ S = serialize ($ a); // serialize object
$ Fp = fopen ("store", "w"); // open a file in the "w" mode and obtain the File Handle. pay attention to the directory attributes here.
Fputs ($ fp, $ s); // write a file
Fclose ($ fp); // close the file handle
?>
// ************** B. php deserializes the object and calls its method **************
Require_once ("class. php"); // The require class. php file is required because the serialization object cannot serialize its method.
$ S = implode ("", @ file ("store "));
$ A = unserialize ($ s );
$ A-> show_var_value ();
?>
The above example is successfully debugged on the win2k server apache2.0.36 php4.2.1
Serialization can only be used when users are limited. because you need to create a file for each user, you need to create directory permissions and ensure that the file name cannot be repeated. the user cannot exit normally.
In the case of a browser, the file cannot be deleted, so this method is more convenient for background management.
When there are a large number of users, we can use sessions to save objects. because session files are managed by the system, we do not have to create a file,
The deletion and directory permissions are troublesome. example:
// ******************* A1.php registers the object as the session variable ************ ****
Require_once ("class. php ");
Session_start ();
$ _ SESSION ["a"] = new a ("hello,", "world! ");
?>
// ******************* B1.php use session variable *************** *
Require_once ("class. php"); // similarly, when the registered object is a session variable, the method cannot be saved. Therefore, the require class. php file is required.
Session_start ();
$ _ SESSION ["a"]-> show_var_value ();
?>
The above example is successfully debugged in win98se apache2.0.36 php4.2.1
Appendix: now we will serialize the object and paste the file registered as the session variable as follows. I hope someone can analyze it for your reference.
Note: The above article without copyright protection, can be arbitrarily modified, spread. if you have any objection, please mailto: chensiping@sina.com.
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.