Serialize() AndUnserialize() The explanation in the PHP manual is:
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
<? PHP
// 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 '<Br>';
/*
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 <br>" ;
Print '<Br>' ;
?>