Manuel PHP

The Serializable interface

Introduction

Interface for customized serializing.

Classes that implement this interface no longer support __sleep() and __wakeup(). The method serialized is called whenever an instance needs to be serialized. This does not invoke __destruct() or has any other side effect unless programmed inside the method. When the data is unserialized the class is known and the appropriate unserialize() method is called as a constructor instead of calling __construct(). If you need to execute the standard constructor you may do so in the method

Synopsis de la classe

Serializable
Serializable {
/* Methods */
abstract public string Serializable::serialize ( void )
abstract public mixed Serializable::unserialize ( string $serialized )
}

Exemple #1 Basic usage

  1. <?php
  2. class obj implements Serializable { 
  3.    private $data; 
  4.    public function __construct() { 
  5.       $this->data = "My private data"; 
  6.    } 
  7.    public function serialize() { 
  8.       return serialize($this->data); 
  9.    } 
  10.    public function unserialize($data) { 
  11.       $this->data = unserialize($data); 
  12.    } 
  13.    public function getData() { 
  14.       return $this->data; 
  15.    }  
  16. }  
  17.  
  18. $obj = new obj;  
  19. $ser = serialize($obj);  
  20.  
  21. $newobj = unserialize($ser);  
  22.  
  23. var_dump($newobj->getData());  
  24. ?> 

L'exemple ci-dessus va afficher quelque chose de similaire à :

string(15) "My private data"

Sommaire


Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Bretagne - php - Moto - Kit graphique