You can prevent an object getting unserialized by returning NULL. Instead of a serialized object, PHP will return the serialized form of NULL:
<?php
class testNull implements Serializable {
public function serialize() {
return NULL;
}
public function unserialize($data) {
}
}
$obj = new testNull;
$string = serialize($obj);
echo $string; // "N;"
?>
That's perhaps better than throwing exceptions inside of the serialize function if you want to prevent serialization of certain objects.
La interfaz Serializable
Introducción
Interfaz para personalizar la serialización.
Las clases que implementan esta interfaz no soportan __sleep() ni __wakeup(). El método serialize se llama cuando una instancia requiere ser serializada. Esto no invoca __destruct() ni tiene ningún efecto adicional a menos que se programe dentro del método. Cuando los datos son desserializados, la clase es conocida y el correspondiente método unserialize() es llamado como constructor en lugar de llamar al método __construct(). Se puede ejecutar el constructor estándar en el método si fuera necesario.
Interfaz synopsis
Serializable
{
/* Métodos */
}Example #1 Basic usage
<?php
class obj implements Serializable {
private $data;
public function __construct() {
$this->data = "My private data";
}
public function serialize() {
return serialize($this->data);
}
public function unserialize($data) {
$this->data = unserialize($data);
}
public function getData() {
return $this->data;
}
}
$obj = new obj;
$ser = serialize($obj);
$newobj = unserialize($ser);
var_dump($newobj->getData());
?>
El resultado del ejemplo sería algo similar a:
string(15) "My private data"
Table of Contents
- Serializable::serialize — Representación en formato cadena de un objecto
- Serializable::unserialize — Construye el objecto
Anonymous
12-May-2011 01:09
