serializeの履歴

PHPでオブジェクトや配列などをバイナリ化する。unserializeで元に戻せる。
オブジェクトにはメソッドの情報は入らない。あくまで変数のみ。

hoge.php

<?php
class hoge{
    private $priv = 1;
    protected $prot = 2;
    public $pub = 3;
    function moge(){
        echo 'this is ' . get_class($this);
    }
}
$h = new hoge();
file_put_contents('hoge.ser', serialize($h));
echo $h->moge();


use_hoge.php

<?php
//require_once('hoge.php');
$hoge = unserialize(file_get_contents('hoge.ser'));

echo $hoge->moge();
var_dump($hoge);


includeしてなかったらこう言われる

Fatal error: main(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "hoge" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition