Object Serialization

Object serialization in PHP is very easy, and can be used for a variety of different purposes.

how to convert an object to a string, and vice-versa.
We can convert object to string through serialize() function and return string to object through unserialize() function.

Note:-(i) When serializing an object, PHP only stores the object’s current state, i.e. its property values. It does not serialize its methods.
(ii) When serializing an object, value store in byte-stream format.

serialization

Which situation we use serialization of the object:-
(i) Passing objects via fields in web forms.
(ii) Passing objects in URL query strings.
(iii) Storing object data in a text file, or in a single database field.

Example:-
In this eg. we have 2 files, in the first file we serialize the object of class student and in display.php we unserialize of the string variable which is created in student.php

(i) student.php

<?php
class student {
	public $stu_name = 'John';
	function display_name() {
		return $this -> stu_name;
	}

}

$stu_obj = new student;
$stu = serialize($stu_obj);
?>

(ii) display.php

<?php
include('student.php');
$stu_info = unserialize($stu);
echo $stu_info -> display_name();
?>
Output:- John