CakeFest 2024: The Official CakePHP Conference

オブジェクトのシリアライズ

オブジェクトのシリアライズ - セッション内でのオブジェクト

serialize() は、 PHP で保存できるあらゆる値をバイトストリームで表した文字列を返します。 unserialize() を使うと、 この文字列から元の変数の値を取り出すことができます。 オブジェクトをシリアライズすると、オブジェクト内の変数もすべて保存されます。 オブジェクト内のメソッドは保存されません。 クラス名のみが保存されます。

オブジェクトを unserialize() するには、 そのオブジェクトのクラスが定義されている必要があります。 A クラスのオブジェクトをシリアライズしたのなら、 その文字列にはクラス A とその中のすべての変数の値が含まれています。 別のファイルでそれを復元してクラス A のオブジェクトを取り出すには、 まずそのファイル内にクラス A の定義が存在しなければなりません。 これを実現するには、たとえばクラス A の定義を別ファイルに書き出してそれを include したり spl_autoload_register() 関数を使ったりします。

<?php
// A.php:

class A {
public
$one = 1;

public function
show_one() {
echo
$this->one;
}
}

// page1.php:

include "A.php";

$a = new A;
$s = serialize($a);
// $s を、page2.php からみつけられるどこかに書き出します
file_put_contents('store', $s);

// page2.php:

// シリアライズした文字列を復元するには、これが必要です
include "A.php";

$s = file_get_contents('store');
$a = unserialize($s);

// これで、$a オブジェクトの show_one() が使えるようになりました
$a->show_one();
?>

アプリケーション内でオブジェクトをシリアライズして再利用する場合のお勧めは、 そのクラスの定義をアプリケーション全体で include することです。 クラスの定義が存在しなければオブジェクトの復元に失敗してしまいます。 その場合、PHP は __PHP_Incomplete_Class_Name クラスのオブジェクトを返します。このオブジェクトにはメソッドは一切なく、 使い道がなくなってしまいます。

つまり、もし上の例の $a を スーパーグローバル $_SESSION に新しいキーを追加してセッションに格納するなら、 page1.phppage2.php だけではなく すべてのページで A.php を include しなければなりません。

ここまでで説明した以外の方法として、オブジェクトのシリアライズや復元のイベントを __sleep() メソッドと __wakeup() メソッドでフックすることができます。 __sleep() を使うと、 オブジェクトの一部のプロパティだけをシリアライズすることもできます。

add a note

User Contributed Notes 4 notes

up
229
php at lanar dot com dot au
14 years ago
Note that static members of an object are not serialized.
up
29
michael at smith-li dot com
9 years ago
Reading this page you'd be left with the impression that a class's `serialize` and `unserialize` methods are unrelated to the `serialize` and `unserialize` core functions; that only `__sleep` and `__unsleep` allow you to customize an object's serialization interface. But look at http://php.net/manual/en/class.serializable.php and you'll see that there is a more straightforward way to control how a user-defined object is serialized and unserialized.
up
3
Anonymous
3 years ago
Until such time as these documents are updated, note that `session_register()` is not needed to automatically serialize & unserialize objects in sessions. Any objects in `$_SESSION` are serialized when the session is closed (via `session_write_close()` or upon script exit) & unserialized when opened (via `session_start()`). The note about including classes throughout an app (either by including the definition globally or autoloading it) still holds.
up
-11
Yahia Fouda
1 year ago
This trick can be useful when you need to pass object data as strings of text between scripts and applications. Common situations include:

* Passing objects via fields in web forms
* Passing objects in URL query strings
* Storing object data in a text file, or in a single database field

and Sometimes it’s useful to do some cleaning up before serializing an object. For example, you might want to write unsaved object data to a database and close the database connection. Similarly, after you’ve unserialized an object, you might want to restore its database connection and perform other setup tasks so that the new object can be used properly.
To Top