downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Serializable::serialize> <ArrayAccess::offsetUnset
Last updated: Fri, 20 May 2011

view this page in

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 */
abstract public string serialize ( void )
abstract public mixed unserialize ( string $serialized )
}

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



add a note add a note User Contributed Notes Serializable
Anonymous 12-May-2011 01:09
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.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites