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
[edit] Last updated: Fri, 23 Mar 2012

view this page in

Serializable arayüzü

(PHP 5 >= 5.1.0)

Giriş

Dizgeleştirmeyi özelleştirme arayüzü.

Bu arayüzü gerçekleyen sınıflar __sleep() ve __wakeup() işlevlerini desteklemezler. Bir örneğin dizgeleştirilmesi gerektiğinde serialize yöntemi çağrılır. Bunun __destruct() çağırmak veya yöntem içinde tanımlanmamış bir işlemi yapmak gibi bir yan etkisi yoktur. Dizgeleştirilmiş nesne yeniden oluşturulmak istendiğinde sınıf bilinmekte olur ve kurucu olarak __construct() yerine ilgili unserialize() yöntemi çağrılır. Eğer standart kurucuyu çalıştırmak isterseniz bunu yöntemin içinde yapmanız gerekir.

Arayüz Sözdizimi

Serializable {
/* Yöntemler */
abstract public string serialize ( void )
abstract public mixed unserialize ( string $dizgeleşmiş )
}

Örnek 1 Temel kullanım

<?php
class obj implements Serializable {
    private 
$data;
    public function 
__construct() {
        
$this->data "Özel verim";
    }
    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());
?>

Yukarıdaki örnek şuna benzer bir çıktı üretir:

string(11) "Özel verim"

İçindekiler



Serializable::serialize> <ArrayAccess::offsetUnset
[edit] Last updated: Fri, 23 Mar 2012
 
add a note add a note User Contributed Notes Serializable
marcos dot gottardi at folha dot REM0VE-THIS dot com dot br 13-Jan-2012 06:05
Serializing child and parent classes:

<?php
class MyClass implements Serializable {
    private
$data;
   
    public function
__construct($data) {
       
$this->data = $data;
    }
   
    public function
getData() {
        return
$this->data;
    }
   
    public function
serialize() {
        echo
"Serializing MyClass...\n";
        return
serialize($this->data);
    }
   
    public function
unserialize($data) {
        echo
"Unserializing MyClass...\n";
       
$this->data = unserialize($data);
    }
}

class
MyChildClass extends MyClass {
    private
$id;
    private
$name;
   
    public function
__construct($id, $name, $data) {
       
parent::__construct($data);
       
$this->id = $id;
       
$this->name = $name;
    }
   
    public function
serialize() {
        echo
"Serializing MyChildClass...\n";
        return
serialize(
            array(
               
'id' => $this->id,
               
'name' => $this->name,
               
'parentData' => parent::serialize()
            )
        );
    }
   
    public function
unserialize($data) {
        echo
"Unserializing MyChildClass...\n";
       
$data = unserialize($data);
       
       
$this->id = $data['id'];
       
$this->name = $data['name'];
       
parent::unserialize($data['parentData']);
    }
   
    public function
getId() {
        return
$this->id;
    }
   
    public function
getName() {
        return
$this->name;
    }
}

$obj = new MyChildClass(15, 'My class name', 'My data');

$serial = serialize($obj);
$newObject = unserialize($serial);

echo
$newObject->getId() . PHP_EOL;
echo
$newObject->getName() . PHP_EOL;
echo
$newObject->getData() . PHP_EOL;

?>

This will output:

Serializing MyChildClass...
Serializing MyClass...
Unserializing MyChildClass...
Unserializing MyClass...
15
My class name
My data
Anonymous 05-Jul-2011 04:01
You cannot throw an exception inside the serialize() method.  This will cause PHP to complain that you are not returning a string or NULL.

The best way to prevent the serialization of an object is to throw an Exception in the __sleep() method:

<?php
class Obj {
  public function
__sleep() {
    throw new
BadMethodCallException('You cannot serialize this object.');
  }
}
?>
Anonymous 12-May-2011 03: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