<?php
class User {
private $name;
private $pass;
function __construct($name, $pass) {
$this->name = $name;
$this->pass = $pass;
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getPass() {
return $this->pass;
}
public function setPass($pass) {
$this->pass = $pass;
}
}
?>
以它來實體化一個新的$user物件,
<?php $user = new User("Jerry", "adioirq386hsakhd"); echo $user->getName(); ?>
然後我們使用serialize函數將$user物件序列化為字串,
<?php //物件的序列化 $user_str = = serialize($user); ?>
最後可以利用unserialize函數把$user_str字串反序列化為$obj物件。
<?php //物件的反序列化 $obj = unserialize($user_str); ?>