Nesne Dizgeleştirme

Oturumlardaki nesnelerin dizgeleştirilmesi

serialize() işlevi PHP'de saklanmış bir değerin bayt-akımı gösterimini içeren bir dizge döndürür. Özgün değişken değerlerini yeniden oluşturmak için unserialize() işlevi kullanılabilir. Bir nesneyi saklamak için dizgeleştirme kullanımı nesnenin içindeki tüm değişkenleri saklar. Nesnenin yöntemleri saklanmaz sadece sınıf ismi saklanır.

Bir nesne için unserialize() kullanabilmek için sınıf nesnesinin tanımlı olması gerekir. Yani, A sınıfının bir nesnesi varsa ve dizgeleştirilirse, A sınıfının ismini ve içerdiği tüm değişkenlerin değerlerini içeren bir dizge elde edilir. Başka bir dosyada bu dizge unserialize() ile dönüştürülmek istenirse, bu dosyada daha önce A sınıfı tanımlanmış ve nesnesi oluşturulmuş olmalıdır. Bu, örneğin, A sınıfının tanımı bir include dosyasında saklanıp bu dosya betiğe dahil edilerek veya spl_autoload_register() işlevi kullanılarak yapılabilir.

<?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'i page2.php'nin bulabileceği bir yere sakla
file_put_contents('store', $s);

// page2.php:

// unserialize işlevinin düzgün çalışması için gerekli
include "A.php";

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

// şimdi show_one() işlevi kullanılabilir.
$a->show_one();
?>

Bir uygulama, uygulamada daha sonra kullanmak için nesneleri dizgeleştiriyorsa, uygulamanın o nesnenin sınıf tanımını uygulama boyunca içermesi şiddetle tavsiye edilir. Bunu yapmamak, bir nesnenin sınıf tanımlaması olmaksızın nesneleştirilmesiyle sonuçlanabilir, bu da PHP'nin nesneyi hiçbir yöntemi olmayan bir __PHP_Incomplete_Class_Name sınıfına sokmasına neden olur ve sonuçta nesne işe yaramaz hale gelir.

Yani, yukarıdaki örnekte, eğer $_SESSION süper küresel dizisine yeni bir anahtar ekleyerek $a bir oturumun parçası haline gelirse, A.php dosyası yalnız page1.php ve page2.php dosyalarına değil tüm sayfa dosyalarına dahil edilmelidir.

Yukarıdaki tavsiyenin ötesinde, __sleep() ve __wakeup() yöntemlerini kullanarak da bir nesne üzerinde dizgeleştirme ve yeniden nesneleştirme yapabilir. __sleep() kullanımı ayrıca, nesne özelliklerinin tamamının değil sadece bir kısmının dizgeleştirilmesini de sağlar.

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