<?php

/* Marshal is class for serialize object data.
* This is just wrapper of serialize/unserialize,
* and just add a bit support save/load to file.
*
* EXAMPLES
*
*    # dump data and save it to file.
*    $name = "hina chan";
*    $age = 15;
*    Marshal::save("dumped_file", array("Name" => $name, "Age" => $age));
*
*    # load data from file.
*    $data = Marshal::load("dumped_file");
*/

# PHP 4.3 later
if (! function_exists('file_get_contents')) {
    function 
file_get_contents($filename$use_include_path=0) {
        
$data NULL;
        
$file = @fopen($filename"rb"$use_include_path);
        if (
$file) {
            while (!
feof($file)) {
                
$data .= fread($file1024);
            }
            
fclose($file);
        }
        return 
$data;
    }
}

# PHP 5 CVS only
if (! function_exists('file_put_contents')) {
    function 
file_put_contents($filename$content) {
        
$file = @fopen($filename"w+");

        if (! 
$file)
            return 
false;
    
        if (! 
fwrite($file$content))
            return 
false;
            
        
fclose($file);
        return 
true;
    }
}

class 
Marshal
{
    function 
load($file) {
        if (
file_exists($file) and is_readable($file))
            return 
unserialize(file_get_contents($file));
        else
            return 
NULL;
    }

    function 
dump($data) {
        return 
serialize($data);
    }

    function 
save($file$data) {
        if (
file_exists($file) and is_writeable($file))
            return 
file_put_contents($fileMarshal::dump($data));
        else
            return 
false;
    }
}

?>