The function serialize () returns a string containing a byte-stream representation of any value that can be stored in PHP. Using serialize to save an object will save all variables in an object.

NOTE: The methods in an object will not be saved, only the name of the class is saved.

Syntax for serialization

<?php
    // define class with variables and methods
    class className {
        public $var = "value here...";
        public function func() {
            // code here...
        }
    }
    // call class
    $class = new className();
    $ser = serialize($class);
?>

Example with object serialization

<?php
    // define class with variables and methods
    class className {
        public $var = "variable";
        public function func() {
            echo $this -> $var;
        }
    }
    // call class
    $class = new className();
    $ser = serialize($class); // store somewhere
?>

 

›› go to examples ››