View Problem
Implement and use an Interface
Create a Serializable interface consisting of
* Accept a stream or handle or descriptor argument for the source or destination
* Save to destination or restore from source the properties or data members of the implementing class (restrict yourself to the primitive types
Next, create a Person class which has
Submit a new solution for php
There are 13 other solutions in additional languages (clojure, cpp, fantom, fsharp ...)
'save' and 'restore' methods, each of which:
* Accept a stream or handle or descriptor argument for the source or destination
* Save to destination or restore from source the properties or data members of the implementing class (restrict yourself to the primitive types
'int' and 'string')
Next, create a Person class which has
'name' and 'age' properties or data members and implements this interface. Instantiate a Person object, save it to a serial stream, and instantiate a new Person object by restoring it from the serial stream.
php 5 (tested in 5.3)
class Person implements Serializable {
private $name;
private $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function serialize() {
return serialize(array($this->name, $this->age));
}
public function unserialize($serialized) {
list($this->name, $this->age) = unserialize($serialized);
}
public function getData() {
return array($this->name, $this->age);
}
}
$obj = new Person('Gaylord Focker', 21);
file_put_contents('person.dump', serialize($obj));
$newobj = unserialize(file_get_contents('person.dump'));
var_dump($newobj->getData());
private $name;
private $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function serialize() {
return serialize(array($this->name, $this->age));
}
public function unserialize($serialized) {
list($this->name, $this->age) = unserialize($serialized);
}
public function getData() {
return array($this->name, $this->age);
}
}
$obj = new Person('Gaylord Focker', 21);
file_put_contents('person.dump', serialize($obj));
$newobj = unserialize(file_get_contents('person.dump'));
var_dump($newobj->getData());
Submit a new solution for php
There are 13 other solutions in additional languages (clojure, cpp, fantom, fsharp ...)




