The php functions serialize () and unserialize () are described in the php manual as follows:
Serialize? Generates a storable representation of a value
Serialize? Generate a representation of a stored value
Unserialize? Creates a PHP value from a stored representation
Unserialize? Create a PHP value from a stored representation
// Declare a class
Class dog {
Var $ name;
Var $ age;
Var $ owner;
Function dog ($ in_name = "unnamed", $ in_age = "0", $ in_owner = "unknown "){
$ This-> name = $ in_name;
$ This-> age = $ in_age;
$ This-> owner = $ in_owner;
}
Function getage (){
Return ($ this-> age * 365 );
}
Function getowner (){
Return ($ this-> owner );
}
Function getname (){
Return ($ this-> name );
}
}
// Instantiate this class
$ Ourfirstdog = new dog ("Rover", 12, "Lisa and Graham ");
// Use the serialize function to convert this instance into a serialized string
$ Dogdisc = serialize ($ ourfirstdog );
Print $ dogdisc; // $ ourfirstdog has been serialized as a string O: 3: "dog": 3: {s: 4: "name"; s: 5: "Rover "; s: 3: "age"; I: 12; s: 5: "owner"; s: 15: "Lisa and Graham ";}
Print'
';
/*
Bytes -----------------------------------------------------------------------------------------
Here you can store the string $ dogdisc anywhere, such as session, cookie, database, and PHP files.
Bytes -----------------------------------------------------------------------------------------
*/
// Cancel this class here
Unset ($ ourfirstdog );
/* Restore operation */
/*
Bytes -----------------------------------------------------------------------------------------
Here, you can read the string $ dogdisc from your storage location, such as session, cookie, database, and PHP files.
Bytes -----------------------------------------------------------------------------------------
*/
// Here we use unserialize () to restore serialized objects.
$ Pet = unserialize ($ dogdisc); // $ pet is the previous $ ourfirstdog object.
// Get the age and name attributes
$ Old = $ pet-> getage ();
$ Name = $ pet-> getname ();
// This class can be used without instantiation at this time, and both attributes and values remain in the state before serialization.
Print "Our first dog is called $ name and is $ old days old
";
Print'
';
?>